Skip to main content

Lua Table Library

table

Standard Lua 5.1 table library for building and manipulating array-style tables (insert/remove/sort/concat). Node trees returned by linkiir.data are their own object type, not plain tables — use the Node:* methods for those.


table.insert

function

table.insert(t, [pos,] value)

Insert an element into a table.

Inserts value at position pos in table t, shifting up subsequent elements. pos defaults to #t + 1 (append to the end).

Usage

table.insert(t, pos, value)

Parameters

NameTypeRequiredDescription
ttableYesTable to modify.
posintegerNo1-based insert position. Default #t + 1.
valueanyYesValue to insert.

Returns

  • none (mutates t)

Example

local Items = { 'a', 'b' }
table.insert(Items, 'c') -- { 'a', 'b', 'c' }
table.insert(Items, 1, 'z') -- { 'z', 'a', 'b', 'c' }

table.remove

function

table.remove(t [, pos])

Remove an element from a table.

Removes from table t the element at position pos, shifting down subsequent elements, and returns the removed value. pos defaults to #t (remove the last element).

Usage

table.remove(t, pos)

Parameters

NameTypeRequiredDescription
ttableYesTable to modify.
posintegerNo1-based position to remove. Default #t.

Returns

  • the removed value

Example

local Items = { 'a', 'b', 'c' }
local Last = table.remove(Items) -- "c"; Items = { 'a', 'b' }
local First = table.remove(Items, 1) -- "a"; Items = { 'b' }

table.concat

function

table.concat(t [, sep [, i [, j]]])

Join array elements into a string.

Returns t[i] .. sep .. t[i+1] .. sep .. ... .. t[j]. sep defaults to the empty string; i defaults to 1; j defaults to #t.

Usage

table.concat(t, sep, i, j)

Parameters

NameTypeRequiredDescription
ttableYesTable (array) of strings/numbers.
sepstringNoSeparator. Default "".
iintegerNoStart index. Default 1.
jintegerNoEnd index. Default #t.

Returns

  • string

Example

local Ids = { '1', '2', '3' }
print(table.concat(Ids, ',')) -- "1,2,3"

table.sort

function

table.sort(t [, comp])

Sort a table's array part in place.

Sorts the elements of table t (the array part) in place, from t[1] to t[#t]. comp is an optional less-than comparator function; the default is the standard \< operator.

Usage

table.sort(t, comp)

Parameters

NameTypeRequiredDescription
ttableYesTable (array) to sort.
compfunctionNoComparator: function(a, b) returning true when a should come before b.

Returns

  • none (mutates t)

Example

local Nums = { 3, 1, 2 }
table.sort(Nums)
print(table.concat(Nums, ',')) -- "1,2,3"

table.sort(Nums, function(a, b) return a > b end) -- descending

table.maxn

function

table.maxn(t)

Largest positive numeric key in a table.

Returns the largest positive numerical index of table t, or 0 if t has no positive numerical indices. Useful for arrays with holes, where #t is undefined.

Usage

table.maxn(t)

Parameters

NameTypeRequiredDescription
ttableYesTable to inspect.

Returns

  • integer

Example

local Sparse = { [1] = 'a', [5] = 'b' }
print(table.maxn(Sparse)) -- 5