Lua Tables as Arrays
Lua has no separate array type. An array (or list) is a table whose keys are the consecutive integers 1, 2, 3, …, n. Lua calls this a sequence, and the length operator #, ipairs and the table library are all built for it.
local fruits = { "apple", "banana", "cherry" }
print(fruits[1]) -- indexing starts at 1
print(fruits[#fruits]) -- the last element
print(fruits[0], fruits[4])
print(#fruits)apple
cherry
nil nil
3
Reading past the end is not an error, it just gives nil.
Adding and changing elements
local queue = { "Ana", "Ben" }
queue[#queue + 1] = "Cleo" -- append (the idiomatic way)
table.insert(queue, "Dev") -- append with the table library
table.insert(queue, 1, "Zed") -- insert at position 1, shifting the rest
queue[2] = "ANA" -- replace
print(#queue, table.concat(queue, ", "))5 Zed, ANA, Ben, Cleo, Dev
Looping
local temps = { 18.5, 21.0, 19.25, 23.75 }
-- numeric for: you control the index
for i = 1, #temps do
print(i, temps[i])
end
-- ipairs: index and value, in order
local sum = 0
for _, t in ipairs(temps) do sum = sum + t end
print("average", sum / #temps)
-- backwards
for i = #temps, 1, -1 do io.write(temps[i], " ") end
print()1 18.5
2 21.0
3 19.25
4 23.75
average 20.625
23.75 19.25 21 18.5
Holes break the length
#t is only reliable for a proper sequence. If the table has a "hole" (a nil in the middle), #t may return the position before any hole, and ipairs stops at the first one:
local list = { 10, 20, 30, 40 }
list[2] = nil -- creates a hole
local seen = 0
for _ in ipairs(list) do seen = seen + 1 end
print("ipairs saw", seen)
table.remove({}, 1) -- harmless on an empty table
local clean = { 10, 20, 30, 40 }
table.remove(clean, 2) -- removes AND shifts: no hole
print(#clean, table.concat(clean, " "))ipairs saw 1
3 10 30 40
Warning: Never set an element in the middle of a list to
nil. Usetable.remove(t, i), which shifts the following elements down.
Two-dimensional arrays
A matrix is a list of rows, where each row is a list:
local rows, cols = 3, 4
local grid = {}
for r = 1, rows do
grid[r] = {}
for c = 1, cols do
grid[r][c] = (r - 1) * cols + c
end
end
for r = 1, rows do
print(table.concat(grid[r], "\t"))
end
print("centre-ish:", grid[2][3])1 2 3 4
5 6 7 8
9 10 11 12
centre-ish: 7
Common array helpers
local function indexOf(list, value)
for i, v in ipairs(list) do
if v == value then return i end
end
return nil
end
local function reverse(list)
local out = {}
for i = #list, 1, -1 do out[#out + 1] = list[i] end
return out
end
local function slice(list, from, to)
return { table.unpack(list, from, to) }
end
local letters = { "a", "b", "c", "d", "e" }
print(indexOf(letters, "c"), indexOf(letters, "z"))
print(table.concat(reverse(letters)))
print(table.concat(slice(letters, 2, 4)))3 nil
edcba
bcd
Starting at 0?
You can store something at index 0 (t[0] = "x"), but #, ipairs, table.insert, table.concat and table.unpack all ignore it. Stick with 1-based lists; it is what every Lua library expects.
Test yourself
What is the idiomatic way to append v to list t?
t.push(v)t[#t + 1] = vt[#t] = vt + v
What does ipairs do when it reaches a nil element?
- Skips it
- Stops
- Raises an error
- Returns 0
How should you delete the element at position 3 of a list?
t[3] = niltable.remove(t, 3)t:delete(3)#t = #t - 1