Lua Tables
The table is Lua's one and only data structure. A table maps keys to values: keys can be any value except nil (and NaN), values can be anything. Arrays, dictionaries, sets, records, objects, modules and namespaces are all tables.
local book = {} -- an empty table
book["title"] = "Dune" -- key "title"
book.author = "Frank Herbert" -- same as book["author"]
book[1] = "first chapter" -- numeric key
book[true] = "a boolean key"
print(book.title, book["author"])
print(book[1], book[true])
print(book.pages) -- missing key: nilDune Frank Herbert
first chapter a boolean key
nil
Table constructors
{ … } builds a table. Three kinds of entries can be mixed:
| Entry | Meaning | Example |
|---|---|---|
value |
next list position (1, 2, 3, …) | { "a", "b" } |
name = value |
string key that is a valid name | { x = 10 } |
[expr] = value |
any key | { ["two words"] = 1, [10] = "ten" } |
local mixed = {
"red", "green", -- [1] and [2]
size = "large", -- ["size"]
["top speed"] = 220, -- key with a space
[10] = "ten",
}
print(mixed[1], mixed[2], mixed.size)
print(mixed["top speed"], mixed[10])
print(#mixed)red green large
220 ten
2
#mixed is 2: the length operator counts only the sequence part (keys 1, 2, … without gaps).
Tables are references
A variable holds a reference to a table, not a copy. Assigning or passing a table shares the same object:
local original = { score = 10 }
local alias = original
alias.score = 99
print(original.score)
local function reset(t) t.score = 0 end
reset(original)
print(alias.score)
local other = { score = 0 }
print(original == alias, original == other)99
0
true false
To get an independent copy, copy the fields yourself:
local function shallowCopy(t)
local copy = {}
for k, v in pairs(t) do copy[k] = v end
return copy
end
local settings = { volume = 7, muted = false }
local backup = shallowCopy(settings)
settings.volume = 2
print(settings.volume, backup.volume)2 7
A shallow copy shares any nested tables. A deep copy calls itself recursively for table values.
Removing entries
Assign nil to remove a key. There's no separate delete operator:
local stock = { apples = 4, pears = 0, plums = 9 }
stock.pears = nil
local keys = {}
for k in pairs(stock) do keys[#keys + 1] = k end
table.sort(keys)
print(table.concat(keys, ", "))apples, plums
Nested tables
Values can be tables, giving you records of lists, lists of records and so on:
local school = {
name = "Riverside",
classes = {
{ id = "7A", students = { "Ana", "Ben" } },
{ id = "7B", students = { "Cleo", "Dev", "Eli" } },
},
}
for _, class in ipairs(school.classes) do
print(class.id, #class.students, class.students[1])
end
print(school.classes[2].students[3])7A 2 Ana
7B 3 Cleo
Eli
Printing a table
print(t) shows something like table: 0x5583e0b2c6a0: the type and a memory address that changes on each run. To see the contents, write a small helper or use table.concat for lists. The dictionaries lesson includes a reusable dump function.
Test yourself
Which of these is NOT allowed as a table key?
"name"42truenil
After local a = {}; local b = a; b.x = 1, what is a.x?
nil1- An error
{}
How do you remove the key k from table t?
delete t.kt:remove("k")t.k = niltable.delete(t, "k")