Sort a Table[] in Lua

A table in Lua is a set of key-value mappings with unique keys. The pairs are stored in arbitrary order and therefore the table is not sorted in any way. What you can do is iterate over the table in some order. The basic pairs gives you no guarantee of the order in which the … Read more

Search for an item in a Lua list

You could use something like a set from Programming in Lua: function Set (list) local set = {} for _, l in ipairs(list) do set[l] = true end return set end Then you could put your list in the Set and test for membership: local items = Set { “apple”, “orange”, “pear”, “banana” } if … Read more

How do you copy a Lua table by value?

Table copy has many potential definitions. It depends on whether you want simple or deep copy, whether you want to copy, share or ignore metatables, etc. There is no single implementation that could satisfy everybody. One approach is to simply create a new table and duplicate all key/value pairs: function table.shallow_copy(t) local t2 = {} … Read more

tech