Lua Academy · Lesson

table Library Functions

Apply table.insert, remove, sort, concat, move, and unpack.

Lesson 2 of 413 steps

table Library Functions is a free Lua Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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,e

table.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))     -- y

table.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 9

table.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 Lua

table.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,4

table.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  20

table.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: 3

Simulating 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,5

table 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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “table Library Functions” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “table Library Functions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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

  1. math Library Functions
  2. table Library Functions
  3. os and io Libraries
  4. The debug Library
← Back to Lua Academy