Lua’s table library extension

While working on some scripting tutorials for CraftStudio, I needed to extends Lua’s built-in table library which is surprisingly poor (compared to .Net collections manipulation capabilities, for instance).

Hopefully, Lua allows us to extend any object easily. I have implemented a few classic but useful functions that I think are essential to tables/arrays/collections manipulations :

  • table.new (…)
  • table.copy (tableToBeCopied)
  • table.containskey (tableToSearchIn, keyToSearchFor)
  • table.containsvalue (tableToSearchIn, valueToSearchFor)
  • table.print (tableToDisplay)
  • table.length (table[, keyType=”both”])
  • table.join (…)
  • table.compare (table1, table2)

Also, creating tables with table.new() allows you to use a syntactic sugar as you can call the functions of the table library on any of these tables, and not only on the table global variable. Ie : t:containsvalue(5)  t:insert(5)

Update July 2013 : This extension of the Lua’s table library is now part of Daneel.

Comments are closed