parrotcode: Lua Table Library | |
Contents | Language Implementations | Lua |
lib/luatable.pir - Lua Table Library
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.
See "Lua 5.1 Reference Manual", section 5.5 "Table Manipulation", http://www.lua.org/manual/5.1/manual.html#5.5.
table.concat (table [, sep [, i [, j]]])
table[i]..sep..table[i+1] ... sep..table[j]
.
The default value for sep
is the empty string,
the default for i
is 1,
and the default for j
is the length of the table.
If i
is greater than j
,
returns the empty string.
table.foreach (table, f)
f
over all elements of table
.
For each element,
f
is called with the index and respective value as arguments.
If f
returns a non-nil value,
then the loop is broken,
and this value is returned as the final value of foreach
.See the next
function for extra information about table traversals.DEPRECATED
table.foreachi (table, f)
f
over the numerical indices of table
.
For each index,
f
is called with the index and respective value as arguments.
Indices are visited in sequential order,
from 1 to n
,
where n
is the size of the table.
If f
returns a non-nil value,
then the loop is broken and this value is returned as the result of foreachi
.DEPRECATED
table.getn (table)
table.insert (table, [pos,] value)
value
at position pos
in table
,
shifting up other elements to open space,
if necessary.
The default value for pos
is n+1
,
where n
is the length of the table,
so that a call table.insert(t,x)
inserts x
at the end of table t
.
table.maxn (table)
table.remove (table [, pos])
table
the element at position pos
,
shifting down other elements to close the space,
if necessary.
Returns the value of the removed element.
The default value for pos
is n
,
where n
is the length of the table,
so that a call table.remove(t)
removes the last element of table t
.
table.setn (table, n)
table.sort (table [, comp])
table[1]
to table[n]
,
where n
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 that not comp(a[i+1],a[i]
) will be true after the sort).
If comp
is not given,
then the standard 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.
|