Function Dump/Table Manipulation: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mindraker
No edit summary
>JulienDethurens
MediaWiki doesn't include the current page in the "map", with subpages. Considering our template is meant to imitate MediaWiki's subpages "map", we should do the same thing. It looks ugly to include the current page, anyways, and it just takes up spa
 
(32 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{CatUp|Function Dump}}
{{Map|Function Dump}}


----


==Table Manipulation==
=Table Manipulation=


This library provides generic functions for table manipulation. It provides all its functions inside the table table.
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.
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]]])'''
===table.concat (table [, sep [, i [, j]]])===




Line 16: Line 15:


{{Example|
{{Example|
<pre>
{{Code and output
t = { "I", "like", "to", "play", "Roblox" }
|code=
print (table.concat (t, ' '))
t = {"I", "like", "to", "play", "Roblox"}
print(table.concat(t, ' '))
|output=I like to play Roblox
}}}}


Will result in:
===<s>table.foreach (table, f)</s>===
I like to play Roblox
</pre>
}}


'''<s>table.foreach</s>'''


This is deprecated. Executes f for each element in table t. It is better to use  
Applies the function f to the elements of the table passed. On each iteration the function f is passed the key-value pair of that element in the table. '''This item is deprecated; use the pairs() function instead!'''


<pre>
===<s>table.foreachi(table, f)</s>===
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  
Applies the function f to the elements of the table passed. On each iteration the function f is passed the index-value pair of that element in the table. This is similar to table.foreach() except that index-value pairs are passed, not key-value pairs. If the function f returns a non-nil value the iteration loop terminates. '''This item is deprecated; use the ipairs() function instead!'''


<pre>
Similar to table.foreach, except that only <b>numeric keys</b> in the range 1 to n are processed.
for k, v in pairs (t) do
  f (k, v)
end
</pre>


{{Example|
<pre>
t = { "I", "like", "to", "play", game = "Roblox" }
table.foreachi (t, print)


Will result in:
===<s>table.getn (table)</s>===
1 I
2 like
3 to
4 play
</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.getn'''
{{Example|
<pre>
a=table.getn { "I", "like", "to", "play", game="Roblox"}
print(a)


Will result in:
Returns the size of the table, when seen as a list. '''This item is deprecated; use the '#' operator instead!'''
4
</pre>
}}


'''table.insert (table, [pos,] value)'''
===table.insert (table, [pos,] value)===




Line 92: Line 46:


{{Example|
{{Example|
<pre>
{{Code and output
t = { "I", "like", "to", "play", "Roblox" }
|code=t = {"I", "like", "to", "play", "Roblox"}
table.insert (t, 2, "do")
table.insert(t, 2, "do")
table.insert (t, 3, "not")
table.insert(t, 3, "not")
print (table.concat (t, ' '))
print(table.concat (t, ' '))
 
|output=I do not like to play Roblox
Will result in:
}}}}
I do not like to play Roblox
</pre>
}}




'''table.maxn (table)'''
===table.maxn (table)===




Line 110: Line 61:


{{Example|
{{Example|
<pre>
{{Code and output
numbers = {0, 1, 2, 3, 4, 5, 6}
|code=numbers = {0, 1, 2, 3, 4, 5, 6}
print(table.maxn (numbers))
print(table.maxn (numbers))
|output=7
}}}}


Will result in:
===table.remove (table [, pos])===
7
</pre>
}}
 
'''table.remove (table [, pos])'''




Line 125: Line 73:


{{Example|
{{Example|
<pre>
{{Code and output
|code=
numbers = {0, 1, 2, 3, 4, 5, 6}
numbers = {0, 1, 2, 3, 4, 5, 6}
print (numbers[4])
print(numbers[4])
table.remove (numbers, 4)
table.remove(numbers, 4)
print (numbers[4])
print(numbers[4])


Will result in:
|output=3 4
3 4
}}}}
</pre>
}}


'''table.sort (table [, comp])'''
===<s>table.setn (t, n)</s>===
 
 
This has been removed from Lua 5.1. Attempting to call it will raise an error. The length of a table can not be set, it is implied by the highest numeric key, providing there are no gaps in the sequence of numeric keys.
 
===table.sort (table [, comp])===




Line 144: Line 96:


{{Example|
{{Example|
<pre>
{{Code and output
t = { "I", "like", "to", "play", "Roblox" }
|code=
table.sort (t)
t = {"I", "like", "to", "play", "Roblox"}
print (table.concat (t, ' '))
table.sort(t)
 
print(table.concat(t, ' '))
Will result in:
|output=I Roblox like play to
I Roblox like play to
}}}}
</pre>
}}


----
==See Also==
[http://www.lua.org/manual/5.1/manual.html#5.5 Lua 5.1 Reference Manual: Table Manipulation]

Latest revision as of 01:23, 24 January 2012


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.

Example
t = {"I", "like", "to", "play", "Roblox"}
print(table.concat(t, ' '))
I like to play Roblox


table.foreach (table, f)

Applies the function f to the elements of the table passed. On each iteration the function f is passed the key-value pair of that element in the table. This item is deprecated; use the pairs() function instead!

table.foreachi(table, f)

Applies the function f to the elements of the table passed. On each iteration the function f is passed the index-value pair of that element in the table. This is similar to table.foreach() except that index-value pairs are passed, not key-value pairs. If the function f returns a non-nil value the iteration loop terminates. This item is deprecated; use the ipairs() function instead!

Similar to table.foreach, except that only numeric keys in the range 1 to n are processed.


table.getn (table)

Returns the size of the table, when seen as a list. This item is deprecated; use the '#' operator instead!

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.

Example
t = {"I", "like", "to", "play", "Roblox"}
table.insert(t, 2, "do")
table.insert(t, 3, "not")
print(table.concat (t, ' '))
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.)

Example
numbers = {0, 1, 2, 3, 4, 5, 6}
print(table.maxn (numbers))
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.

Example
numbers = {0, 1, 2, 3, 4, 5, 6}
print(numbers[4])
table.remove(numbers, 4)
print(numbers[4])
3 4


table.setn (t, n)

This has been removed from Lua 5.1. Attempting to call it will raise an error. The length of a table can not be set, it is implied by the highest numeric key, providing there are no gaps in the sequence of numeric keys.

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.

Example
t = {"I", "like", "to", "play", "Roblox"}
table.sort(t)
print(table.concat(t, ' '))
I Roblox like play to


See Also

Lua 5.1 Reference Manual: Table Manipulation