table
Table functions of Lua 5.1 / LuaJIT 2.1, unchanged by the client, working on the array part of a table (indices 1 to #t).
At a glance
table.pack and table.unpack (Lua 5.2) do not exist: use {...} with select("#", ...) and the global unpack.
Functions
table.concat(t, sep, i, j): string
table.concat(t: table, sep?: string = "", i?: number = 1, j?: number = #t): string
Returns: t[i] .. sep .. t[i+1] ... t[j], "" when i > j.
Errors: invalid value (table) at index 1 in table for 'concat' for an element that is not a string or a number.
print(table.concat({ "a", "b", "c" }, ", "))
table.insert(t, pos, value)
table.insert(t: table, pos?: number, value: any)
With pos, the elements pos to #t move up by one. LuaJIT does not check pos: a value above #t + 1 leaves a hole.
Returns: nothing.
Errors: wrong number of arguments to 'insert' with more than 3 arguments.
local t = { 1, 2, 3 }
table.insert(t, 4)
table.insert(t, 1, 0)
print(table.concat(t, " "))
table.remove(t, pos): any
table.remove(t: table, pos?: number = #t): any
The following elements move down by one.
Returns: the removed value, or nothing when the table is empty or pos is outside 1 to #t.
local queue = { "a", "b", "c" }
print(table.remove(queue, 1), #queue)
table.sort(t, comp)
table.sort(t: table, comp?: function(a: any, b: any): boolean)
Returns: nothing. The sort is not stable.
Errors: invalid order function for sorting for an inconsistent comp (for example one using <=), attempt to compare number with nil and the like without comp.
local t = { 3, 1, 2 }
table.sort(t, function(a, b) return a > b end)
table.move(a1, f, e, t, a2): table
table.move(a1: table, f: number, e: number, t: number, a2?: table = a1): table
Copies a1[f] to a1[e] into a2[t] onward, overlapping ranges included.
Returns: a2.
local copy = table.move({ 1, 2, 3 }, 1, 3, 1, {})
print(#copy)
table.maxn(t): number
table.maxn(t: table): number
Returns: the largest positive numeric key, or 0. It traverses the whole table, so it also works on tables with holes.
table.new(narray, nhash): table
local new_table = require("table.new")
new_table(narray: number, nhash: number): table
Returns: an empty table with that room, which avoids rehashing while it fills.
table.clear(t)
local clear_table = require("table.clear")
clear_table(t: table)
Removes every key but keeps the allocated room, to reuse a table every frame without creating garbage.
Returns: nothing.
Deprecated
Kept for compatibility, prefer the modern forms.
table.getn(t): number
table.getn(t: table): number
Returns: #t.
table.foreach(t, f): any
table.foreach(t: table, f: function(key: any, value: any): any): any
Returns: the first non-nil value returned by f, which also stops the loop. Prefer pairs.
table.foreachi(t, f): any
table.foreachi(t: table, f: function(index: number, value: any): any): any
Returns: the first non-nil value returned by f, which also stops the loop. Prefer ipairs.
Complete example
Keep the 3 nearest entities, reusing one table every frame.
local clear_table = require("table.clear")
local nearest = {}
local function pick(entities, max)
clear_table(nearest)
for _, e in ipairs(entities) do
nearest[#nearest + 1] = e
end
table.sort(nearest, function(a, b) return a.distance < b.distance end)
for i = #nearest, max + 1, -1 do
table.remove(nearest, i)
end
return nearest
end
local list = pick({ { name = "a", distance = 9 }, { name = "b", distance = 2 },
{ name = "c", distance = 5 }, { name = "d", distance = 1 } }, 3)
local names = {}
for i, e in ipairs(list) do names[i] = e.name end
print(table.concat(names, ", "))