Lua: compare multiple values

As explicitly mentioned in the Lua Gotchas Python programmers will naturally be confused about Lua’s way to handle multiple values. Let’s take as an example a function which return multiple values. How can we compare them in an assert context?

function special(i)
  return i + 38, i - 0.8584074

-- 42   3.1415926
-- two values returned by function call

assert(special(4) == 42, 3.1415926)
-- faulty, compare first value with 42
-- "3.1415926" is error message

assert(special(4) == {42, 3.1415926})
-- wrong, hash table is a different data type
-- 42   3.1415926
-- lua: mult.lua:12: assertion failed!
-- stack traceback:
--     [C]: in function 'assert'
--     mult.lua:12: in main chunk
--     [C]: in ?

assert(special(4) == table.unpack{42, 3.1415926})
-- wrong as well, compares only the first value
-- creates a table with two values, but equality compares only one value
-- unpack{...} is short for unpack({...})

But this is a wrong solution as bjoe2k4 points out in the comment section. You need to explicitly write a value comparing function for metatables:

function special(i)
  return i + 38, i - 0.8584074

function tableEquality(t1, t2)
  if #t1 ~= #t2 then
    return false
  for i=1,#t1 do
    if t1[i] ~= t2[i] then
      return false
  return true

actual = {special(4)}
expected = {42, 3.1415926}
print(tableEquality(actual, expected))

Be aware, that this function does not work recursively.

Lua: compare multiple values

HTML5 empty elements

Which elements of HTML5 can be empty? If I got it correct, this is a comprehensive list of elements:

empty_tags = {"area", "base", "basefont", "bgsound", "br", "col", "embed",
    "hr", "img", "input", "isindex", "keygen", "link", "menuitem", "meta",
    "nobr", "param", "source", "track"}

Or as list in HTML:

  • area
  • base
  • basefont
  • bgsound
  • br
  • col
  • embed
  • hr
  • img
  • input
  • isindex
  • keygen
  • link
  • menuitem
  • meta
  • nobr
  • param
  • source
  • track

I extracted this list from MDN and to be honest, it’s rather the list of empty elements supported by Gecko, because e.g. isindex is deprecated.

HTML5 empty elements

Compiling PHP 5.5 with recode support


I had difficulties finding the right location of the recode library at my xubuntu installation. So I would like to share my compilation line:

./configure --with-pear=/usr --enable-mod-charset --with-mysql --enable-debug --disable-short-tags --enable-dtrace --with-openssl --with-pcre-regex --enable-bcmath --enable-calendar --enable-exif --enable-intl --enable-mbstring --with-pspell --with-readline --with-recode=shared,/usr/include/ --enable-sockets --with-xsl --enable-zip

So recode has to be compiled with shared,/usr/include because the second directory contains:

$ locate recode | grep include

Of course you have to install the libraries first. It’s provided in the Ubuntu repositories with the names:

sudo apt-get install librecode-dev librecode0 php5-recode recode

This blog posts shows the base dir where recode is installed at my xubuntu. It fixes the compilation error configure: error: I cannot find recode.h anywhere below yes /usr /usr/local /opt.

Compiling PHP 5.5 with recode support