Table Manipulation¶
Original document, see: http://www.lua.org/manual/5.1/manual.html
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.
table.concat¶
- 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 forsepis the empty string, the default foriis 1, and the default forjis the length of the table. Ifiis greater thanj, returns the empty string.
table.insert¶
- table.insert(table, [pos, ]value)¶
Inserts element
valueat positionposintable, shifting up other elements to open space, if necessary. The default value forposisn+1, wherenis the length of the table, so that a calltable.insert(t,x)insertsxat the end of tablet.
table.maxn¶
- 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.)
table.remove¶
- table.remove(table[, pos])¶
Removes from
tablethe element at positionpos, shifting down other elements to close the space, if necessary. Returns the value of the removed element. The default value forposisn, wherenis the length of the table, so that a calltable.remove(t)removes the last element of tablet.
table.sort¶
- table.sort(table[, comp])¶
Sorts table elements in a given order, in-place, from
table[1]totable[n], wherenis the length of the table.If
compis 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).If
compis 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.
id-1222545