A Mutable Log

A blog by Devendra Tewari


Project maintained by tewarid Hosted on GitHub Pages — Theme by mattgraham

Table manipulation in Lua

In Lua, arrays are tables. Tables can be iterated over using built-in functions such as pairs. Tables can have index values that are non-numeric.

The following example creates two tables, adds contents of one to another, and prints out the data in the resulting table

table1 = {
  ["red"] = {Description="Red", RGB=0xFF0000},
  ["green"] = {Description="Green", RGB=0x00FF00}
}

table2 = {
  ["blue"] = {Description="Blue", RGB=0x0000FF}
}

for color,val in pairs(table1) do
  table2[color] = table1[color]
  -- or table2[color] = val
end

for color in pairs(table2) do
  print(string.format("%06x - %s", table2[color].RGB, table2[color].Description))
end

Here’s how the output looks

0000ff - Blue
00ff00 - Green
ff0000 - Red

The order in which the colors appear is entirely unpredictable.