Lua Advanced Tables Guide — Table Functions and Data Structure Patterns
In this tutorial, you will learn about Lua Advanced Tables Guide. We cover key concepts, practical examples, and best practices to help you master this topic.
Lua advanced tables use the table library for insertion, removal, sorting, and concatenation -- with patterns like sets for uniqueness, queues for FIFO operations, and memoization for Caching function results.
Table Library Functions
local arr = {3, 1, 4, 1, 5, 9}
-- Insert at end
table.insert(arr, 2)
--> {3, 1, 4, 1, 5, 9, 2}
-- Insert at position
table.insert(arr, 1, 0)
--> {0, 3, 1, 4, 1, 5, 9, 2}
-- Remove last
local last = table.remove(arr)
--> last = 2, arr = {0, 3, 1, 4, 1, 5, 9}
-- Remove at position
local first = table.remove(arr, 1)
--> first = 0
-- Sort
table.sort(arr)
--> {1, 1, 3, 4, 5, 9}
-- Custom sort
table.sort(arr, function(a, b) return a > b end)
--> {9, 5, 4, 3, 1, 1}
-- Concatenate
local words = {"hello", "world", "lua"}
print(table.concat(words, ", ")) --> hello, world, lua
Using Tables as Sets
local set = {}
set["apple"] = true
set["banana"] = true
set["cherry"] = true
-- Check membership
if set["apple"] then
print("apple is in set")
end
-- Iterate
for item in pairs(set) do
print(item)
end
Using Tables as Queues
local queue = {}
function enqueue(q, item)
q[#q + 1] = item
end
function dequeue(q)
local item = q[1]
if item then
for i = 2, #q do
q[i - 1] = q[i]
end
q[#q] = nil
end
return item
end
enqueue(queue, "first")
enqueue(queue, "second")
print(dequeue(queue)) --> first
Memoization Pattern
local memo = {}
function fib(n)
if memo[n] then return memo[n] end
if n <= 1 then return n end
memo[n] = fib(n - 1) + fib(n - 2)
return memo[n]
end
print(fib(50)) --> 12586269025
Common Mistakes
1. Modifying table during sort
Do not modify the table inside the sort comparator function.
2. Assuming table.insert is O(1)
Inserting at position 1 shifts all elements. Use a Linked List or deque for large front-insertions.
3. Using # for non-sequential keys
The length operator # only works on arrays (sequential integer keys starting at 1).
Practice Questions
1. How do you remove duplicate values from an array? Use a set pattern: iterate and add to a temporary table, then collect the keys.
2. What does table.concat do? Joins array elements into a string with an optional separator between them.
3. How do you sort a table in reverse order?
Provide a comparator: table.sort(t, function(a, b) return a > b end).
FAQ
{{< faq question="Is table.sort stable?" >}} No, table.sort does not guarantee stable sorting (equal elements may not preserve original order). {{< /faq >}}
{{< faq question="What happens if I table.insert with nil?" >}}
table.insert skips nil values. Use a placeholder like {} or false to preserve positions.
{{< /faq >}}
{{< faq question="How do I copy a table?" >}}
Shallow copy: {table.unpack(t)} for arrays, or iterate manually. Deep copy requires a recursive function.
{{< /faq >}}
What's Next
Now learn about the Lua I/O library.
| Topic | Description | Link |
|---|---|---|
| I/O Library | File and stream operations | {{< ref "20-io" >}} |
| OS Library | System and time functions | {{< ref "21-os-library" >}} |
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro