Table


The table library contains a collection of functions that operate on tables. In most cases these functions expect a specified "kind" of table, most often a list. Whenever possible we try to be explicit about referencing which "kind" of table the function expects.

The table library is always available, and its functions can be accessed by name as shown in the following example:

local list = {
    "d",
    "c",
    "a",
    "b",
}

table.sort(list)

print(list[1]) -- a
print(list[2]) -- b
print(list[3]) -- c
print(list[4]) -- d

The table library contains the following functions:


table.concat (list)
Returns a string containing the concatenation of each element in list.
table.concat (list, sep)
Returns a string containing the concatenation of each element in list separated by sep.
table.concat (list, sep, i)
Returns a string containing the concatenation of each element in list[1:] separated by sep. If i > #list returns an empty string.
table.concat (list, sep, i, j)
Returns a string containing the concatenation of each element in list[i:j] separated by sep. If i > j returns an empty string.

table.insert (list, value)
Appends an element to the end of the list, increasing its length by 1.
table.insert (list, index, value)
Inserts an element into the specified index. Existing elements at positions greater than or equal to index are each shifted to the next higher index, and the length of the list is increased by 1.

table.remove (list)
Removes the last element from the list and returns it, decreasing the length of the list by 1.
table.remove (list, index)
Removes the element at the specified index and returns it. Existing element at positions greater than index are each shifted to the next lower index, and the length of the list is decreased by 1.

table.sort (table)
Sorts list elements in in-place. By default elements are sorted ascending by value. The sort algorithm is "unstable", meaning that elements that evaluate equal may change positions each time table.sort is invoked.
table.sort (table, comp)
Sorts table elements in a given order, in order specified by comp. comp is a function that receives two elements as arguments, then returns true when the first appear in the list prior to the second and false otherwise.