table
Table manipulation functions of Lua 5.1 / LuaJIT 2.1. Most of them work on the array part of a table, indices 1 to #t.
Functions
table.concat(t: table, sep?: string = "", i?: number = 1, j?: number = #t): string
table.concat(t: table, sep?: string = "", i?: number = 1, j?: number = #t): string
- Returns
t[i] .. sep .. t[i+1] ... t[j]. - Elements must be strings or numbers; any other type raises an error.
- Returns an empty string if
i > j.
print(table.concat({ "a", "b", "c" }, ", "))
table.foreach(t: table, f: function): any
table.foreach(t: table, f: function): any
- Calls
f(key, value)for every pair oft, in unspecified order. - Stops and returns the first non-nil value returned by
f. Returns nothing iffnever returns a non-nil value. - Deprecated; prefer
pairs.
table.foreachi(t: table, f: function): any
table.foreachi(t: table, f: function): any
- Calls
f(index, value)for indices 1 to#t, in order. - Stops and returns the first non-nil value returned by
f. - Deprecated; prefer
ipairs.
table.getn(t: table): number
table.getn(t: table): number
- Returns the length of
t. Same as#t. Deprecated.
table.insert(t: table, pos?: number, value: any)
table.insert(t: table, pos?: number, value: any)
table.insert(t, value)appendsvalueat position#t + 1.table.insert(t, pos, value)insertsvalueatposand shifts the elementsposto#tup by one.posmust be in 1 to#t + 1.
local t = { 1, 2, 3 }
table.insert(t, 4)
table.insert(t, 1, 0)
table.maxn(t: table): number
table.maxn(t: table): number
- Returns the largest positive numeric key of
t, or 0 if there is none. It traverses the whole table, so it also works on tables with holes.
table.remove(t: table, pos?: number = #t): any
table.remove(t: table, pos?: number = #t): any
- Removes the element at
posand shifts the following elements down by one. Returns the removed value. - Returns nothing if the table is empty or
posis out of range.
table.sort(t: table, comp?: function)
table.sort(t: table, comp?: function)
- Sorts the elements
t[1]tot[#t]in place. comp(a, b)must returntruewhenamust come beforeb. By default the<operator is used.- The sort is not stable. An inconsistent
comp(for example using<=) can raise an "invalid order function" error.
local t = { 3, 1, 2 }
table.sort(t, function(a, b) return a > b end)