Skip to main content

Table

The table library provides a set of functions for manipulating tables in Lua. Tables in Lua are used to represent arrays, dictionaries, and other data structures.

Functions

table.insert

Insert a value into a table.

Usage:

table.insert(t, value)       -- inserts value at the end of table t
table.insert(t, pos, value) -- inserts value at position pos

table.remove

Remove a value from a table.

Usage:

table.remove(t)      -- removes the last element
table.remove(t, pos) -- removes element at position pos

table.sort

Sort the elements of a table.

Usage:

table.sort(t)              -- sorts in ascending order
table.sort(t, comp) -- sorts using comparison function comp

table.concat

Concatenate the elements of a table into a string.

Usage:

table.concat(t)            -- concatenates all elements
table.concat(t, sep) -- concatenates elements with separator sep
table.concat(t, sep, i, j) -- concatenates elements i through j with sep

table.unpack

Return the elements of a table as separate values.

Usage:

local a, b, c = table.unpack(t)  -- unpacks first elements of t into a, b, c