Function Dump/Table Manipulation: Difference between revisions
>Mindraker <s>table.foreach</s> |
>Mindraker No edit summary |
||
Line 26: | Line 26: | ||
'''<s>table.foreach</s>''' | '''<s>table.foreach</s>''' | ||
This is deprecated. Executes f for each element in table t. It is better to use | |||
<pre> | |||
for k, v in pairs (t) do | |||
f (k, v) | |||
end | |||
</pre> | |||
{{Example| | |||
<pre> | |||
t = { "I", "like", "to", "play", game = "Roblox" } | |||
table.foreach (t, print) | |||
Will result in: | |||
1 I | |||
2 like | |||
3 to | |||
4 play | |||
game Roblox | |||
</pre> | |||
}} | |||
'''<s>table.foreachi</s>''' | |||
This is deprecated. It is better to use | This is deprecated. It is better to use | ||
Line 37: | Line 61: | ||
{{Example| | {{Example| | ||
<pre> | <pre> | ||
t = { "I", "like", "to", "play", "Roblox" } | t = { "I", "like", "to", "play", game = "Roblox" } | ||
table. | table.foreachi (t, print) | ||
Will result in: | Will result in: | ||
Line 45: | Line 69: | ||
3 to | 3 to | ||
4 play | 4 play | ||
</pre> | </pre> | ||
}} | }} | ||
Similar to table.foreach, except that only <b>numeric keys</b> in the range 1 to n are processed. In this example the entry for "game = 'Roblox'" was not returned because it did not have a numeric key.[http://www.gammon.com.au/scripts/doc.php?lua=table.foreachi] | |||
'''table.insert (table, [pos,] value)''' | '''table.insert (table, [pos,] value)''' |
Revision as of 14:54, 15 October 2008
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.
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 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.
t = { "I", "like", "to", "play", "Roblox" } print (table.concat (t, ' ')) Will result in: I like to play Roblox
table.foreach
This is deprecated. Executes f for each element in table t. It is better to use
for k, v in pairs (t) do f (k, v) end
t = { "I", "like", "to", "play", game = "Roblox" } table.foreach (t, print) Will result in: 1 I 2 like 3 to 4 play game Roblox
table.foreachi
This is deprecated. It is better to use
for k, v in pairs (t) do f (k, v) end
t = { "I", "like", "to", "play", game = "Roblox" } table.foreachi (t, print) Will result in: 1 I 2 like 3 to 4 play
Similar to table.foreach, except that only numeric keys in the range 1 to n are processed. In this example the entry for "game = 'Roblox'" was not returned because it did not have a numeric key.[1]
table.insert (table, [pos,] value)
Inserts element 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 (see §2.5.5), so that a call table.insert(t,x) inserts x at the end of table t.
t = { "I", "like", "to", "play", "Roblox" } table.insert (t, 2, "do") table.insert (t, 3, "not") print (table.concat (t, ' ')) Will result in: I do not like to play Roblox
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.)
numbers = {0, 1, 2, 3, 4, 5, 6} print(table.maxn (numbers)) Will result in: 7
table.remove (table [, pos])
Removes from 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.
numbers = {0, 1, 2, 3, 4, 5, 6} print (numbers[4]) table.remove (numbers, 4) print (numbers[4]) Will result in: 3 4
table.sort (table [, comp])
Sorts table elements in a given order, in-place, from 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.
t = { "I", "like", "to", "play", "Roblox" } table.sort (t) print (table.concat (t, ' ')) Will result in: I Roblox like play to