table Library Functions
Apply table.insert, remove, sort, concat, move, and unpack.
table Library Functions is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The table Module
The table module provides functions to manipulate Lua tables used as arrays: insert, remove, sort, concat, move, and unpack.
table.insert
table.insert(t, val) appends to the end. table.insert(t, pos, val) inserts at position, shifting later elements right.
local t = {"a", "b", "d"}
table.insert(t, "e") -- append
table.insert(t, 3, "c") -- insert at index 3
print(table.concat(t, ",")) -- a,b,c,d,etable.remove
table.remove(t) removes and returns the last element. table.remove(t, pos) removes at position and shifts elements left.
local t = {"x", "y", "z"}
print(table.remove(t)) -- z
print(table.remove(t, 1)) -- x
print(table.concat(t)) -- ytable.sort
table.sort(t, cmp) sorts in-place. Without cmp uses <. The comparator must be a strict weak ordering.
local t = {3, 1, 4, 1, 5, 9, 2, 6}
table.sort(t)
print(table.concat(t, " ")) -- 1 1 2 3 4 5 6 9table.concat
table.concat(t, sep, i, j) joins array elements into a string. Elements must be strings or numbers.
local words = {"Hello", "world", "from", "Lua"}
print(table.concat(words, " ")) -- Hello world from Luatable.move
table.move(a1, f, e, t, a2) copies elements from a1[f..e] to a2 starting at t. If a2 is omitted, copies within a1.
local src = {1,2,3,4,5}
local dst = table.move(src, 2, 4, 1, {})
print(table.concat(dst, ",")) -- 2,3,4table.unpack
table.unpack(t, i, j) returns elements t[i] through t[j] as separate values. Default: 1 to #t.
local t = {10, 20, 30}
print(table.unpack(t)) -- 10 20 30
local a, b = table.unpack(t, 1, 2)
print(a, b) -- 10 20table.pack
table.pack(...) packs varargs into a table with a field n holding the total count (including nils).
local function allArgs(...)
local t = table.pack(...)
print("count:", t.n)
return t
end
allArgs(1, nil, 3) -- count: 3Simulating Queue with insert/remove
Use table.insert to enqueue at the end and table.remove(t, 1) to dequeue from the front (note: O(n) cost).
Simulating Stack
Use table.insert to push and table.remove (no position) to pop — both are O(1) operations.
table.move for Shifting
Shift all elements left by one using table.move(t, 2, #t, 1), then nil the last element.
local t = {1,2,3,4,5}
table.move(t, 2, #t, 1)
t[#t] = nil
print(table.concat(t, ",")) -- 2,3,4,5table Library Question
What does table.pack(1, nil, 3) return?
Recap: table Library
The table library provides insert, remove, sort, concat, move, unpack, and pack. Together they handle all common array manipulation needs efficiently.
Frequently asked questions
Is the “table Library Functions” lesson free?
Yes — the full text of “table Library Functions” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “table Library Functions”?
Apply table.insert, remove, sort, concat, move, and unpack. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “table Library Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- math Library Functions
- table Library Functions
- os and io Libraries
- The debug Library