table
A table is a basic Lua type, it implements associative arrays.
An associative array is an array that can be indexed not only with numbers, but also with strings or any other value of the language, except nil.
local t = {} -- create an empty table t["x"] = 10 -- set value == 10 for "x" key t.x = 10 -- does the exact same thing -- ⚠️ -- local x = "aKey" t[x] = 10 -- this sets a value for the "aKey" key
A table may store values with different types of indices and it grows as it needs to accommodate new entries:
local t = {} t.foo = "bar" t[2] = 10 t["test"] = true -- go through all key/value pairs of t for key, value in pairs(t) do print(key, value) end -- prints "foo bar", "2 10", "test true" in an unspecified order
A table can be initialized with values:
local days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} -- days[1] == "Sunday" -- days[2] == "Monday" -- ... local position = {x=1, y=2} -- position.x == 1 -- position.y == 2