Lua Tables

In Lua, everything starts with a table — arrays, dictionaries, sets, objects, and even modules are all implemented using tables. The table type provides essential utility functions for manipulating these powerful data structures.

This post walks you through Lua’s table type in detail: what it does, how to use it effectively, and where it shines.


:brick: What is a Table in Lua?

Tables are associative arrays — they can store values using any kind of key (numbers, strings, even other tables).

local t = { "apple", "banana", "cherry" }
print(t[1])  --> apple

t["color"] = "red"
print(t.color)  --> red

The table type Overview

The table type provides functions for:

  • Inserting/removing elements
  • Concatenating lists
  • Sorting
  • Moving or copying parts of arrays
  • Packing/unpacking variable arguments

Note: The table type only operates on sequences (integer-indexed arrays) unless stated otherwise.


table.insert

Adds a value to the end or at a given position.

local fruits = {"apple", "banana"}
table.insert(fruits, "cherry")         -- Add at end
table.insert(fruits, 2, "orange")      -- Add at index 2

table.remove

Removes and returns an element at a given position (default is last).

local val = table.remove(fruits, 2)    -- Removes "orange"

table.concat

Concatenates elements into a string.

local names = {"Alice", "Bob", "Charlie"}
print(table.concat(names, ", "))   --> Alice, Bob, Charlie

Optional start and end indices:

print(table.concat(names, " | ", 2, 3))  --> Bob | Charlie

table.sort

Sorts a list in-place (ascending by default).

local scores = {10, 5, 30, 20}
table.sort(scores)

With a custom comparator:

table.sort(scores, function(a, b) return a > b end)  -- descending

table.move (Lua 5.3+)

Copies a range of values from one table to another.

local t1 = {"a", "b", "c", "d"}
local t2 = {}
table.move(t1, 2, 4, 1, t2)  -- Copy t1[2] to t1[4] into t2[1]...
-- t2 = {"b", "c", "d"}

Also supports in-place shifting:

table.move(t1, 1, 3, 2, t1)
-- Now t1 = {"a", "a", "b", "c"}

table.pack and table.unpack

For working with variable arguments:

local t = table.pack("a", "b", nil, "c")
print(t.n)  --> 4

local a, b, c, d = table.unpack(t)

table.pack saves the number of elements in t.n, preserving nils.


Common Gotchas

1. #t works only for proper sequences

Lua’s length operator (#) is undefined if your table has gaps (i.e., nil in the middle).

local t = {1, 2, nil, 4}
print(#t)  --> Could be 2 or 4 (undefined behavior)

To count reliably, write a custom iterator or use packed tables.


Looping Over Tables

Numeric (sequential):

for i = 1, #t do
  print(i, t[i])
end

Generic (all keys):

for k, v in pairs(t) do
  print(k, v)
end

Summary of table Functions

Function Description
table.insert Insert value at position
table.remove Remove value at position
table.concat Join values into string
table.sort In-place sort with optional comparator
table.move Copy or shift array slice
table.pack Pack values into table, preserving nils
table.unpack Return table values as multiple arguments

Bonus: Using Tables as Sets

local set = {}
set["apple"] = true
set["banana"] = true

if set["apple"] then
  print("Apple is in the set!")
end