Module table
Table Manipulation This library provides generic functions for table manipulation.
It provides all its functions inside the table table.
Most functions in the table library assume that the table represents an array or a list. For these functions, when we talk about the "length" of a table we mean the result of the length operator.
Type table
table.concat(table, sep, i, j) |
Given an array where all elements are strings or numbers, returns
|
table.insert(table, pos, value) |
Inserts element |
table.maxn(table) |
Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices. |
table.remove(table, pos) |
Removes from |
table.sort(table, comp) |
Sorts table elements in a given order,
in-place, from |
Type table
Field(s)
- table.concat(table, sep, i, j)
-
Given an array where all elements are strings or numbers, returns
table[i]..sep..table[i+1]...sep..table[j]
.The default value for
sep
is the empty string, the default fori
is 1, and the default forj
is the length of the table. Ifi
is greater thanj
, returns the empty string.Parameters
-
#table table
: table to handle. -
#string sep
: the separator, default value is an empty string. -
#number i
: start index, default value is 1. -
#number j
: end index, default value is lenght of the table.
Return value
#string: the concatenated table.
-
- table.insert(table, pos, value)
-
Inserts element
value
at positionpos
intable
, shifting up other elements to open space, if necessary.The default value for
pos
isn+1
, wheren
is the length of the table, so that a calltable.insert(t,x)
insertsx
at the end of tablet
.Parameters
-
#table table
: table to modify. -
#number pos
: index of insertion. -
value
: value to insert.
-
- table.maxn(table)
-
Returns the largest positive numerical index of the given table, or zero if the table has no positive numerical indices.
(To do its job this function does a linear traversal of the whole table.)
Parameter
-
#table table
: table to traverse.
Return value
#number: the largest positive numerical index of the given table, or zero if the table has no positive numerical indices.
-
- table.remove(table, pos)
-
Removes from
table
the element at positionpos
, shifting down other elements to close the space, if necessary.Returns the value of the removed element. The default value for
pos
isn
, wheren
is the length of the table, so that a calltable.remove(t)
removes the last element of tablet
.Parameters
-
#table table
: table to modify. -
#number pos
: index of deletion. (default value is the lenght of the table)
-
- table.sort(table, comp)
-
Sorts table elements in a given order, in-place, from
table[1]
totable[n]
, wheren
is the length of the table.If
comp
is given, then it must be a function that receives two table elements, and returns true when the first is less than the second (so thatnot comp(a[i+1],a[i])
will be true after the sort). Lua operator < is used instead.The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.
Parameters
-
#table table
: table to sort. -
comp
: a function which take to table and returns true when the first is less than the second.
-