Add: Difference between revisions
Jump to navigation
Jump to search
imported>Profi06 (Added optional index parameter) |
No edit summary |
||
| (6 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
{{ApiReference | {{ApiReference | ||
|name=add | |name=add | ||
|shortdesc=Adds | |shortdesc=Adds an element to the end of a sequence in a table. | ||
| | |table||The table. | ||
| | |value||The value to add. | ||
| | |index|optional|The index for the value to be inserted. | ||
|result=The <var>value</var> that was passed in. | |||
}} | }} | ||
The <code>add()</code> function | The <code>add()</code> function adds an element to a sequence in a table. | ||
If an <var>index</var> is not supplied, it is assumed to be <code>#table+1</code> and the value is appended to the end of the table. | |||
The <code>add()</code> function returns the object being added. You can ignore this return value, | The <code>add()</code> function returns the object being added. You can ignore this return value if you don't need it. | ||
A stack can be easily emulated by using <code>add(s,v)</code> to push and <code>[[Deli|v=deli(s)]]</code> to pop. | |||
== Examples == | == Examples == | ||
| Line 18: | Line 21: | ||
add(t, 9) -- t = {1, 5, 7, 9} | add(t, 9) -- t = {1, 5, 7, 9} | ||
add(t, 3, 2) -- t = {1, 3, 5, 7, 9} | add(t, 3, 2) -- t = {1, 3, 5, 7, 9} | ||
-- used to create and keep track of all players | |||
players = {} | |||
function new_player(pad_index, name) | |||
return add(players, {pad_index, name}) | |||
end | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 24: | Line 33: | ||
* [[Lua]] | * [[Lua]] | ||
* [[Tables]] | * [[Tables]] | ||
* [[All| | * <code>[[All|all()]]</code> | ||
* [[Del| | * <code>[[Del|del()]]</code> | ||
* [[Deli| | * <code>[[Deli|deli()]]</code> | ||
* [[Foreach| | * <code>[[Foreach|foreach()]]</code> | ||
* [[Pairs|<code> | * <code>[[Pairs|pairs()]]</code> | ||
* <code>[[IPairs|ipairs()]]</code> | |||
[[Category:Reference]] | [[Category:Reference]] | ||
[[Category:API]] | [[Category:API]] | ||
Latest revision as of 20:38, 22 October 2023
add( table, value, [index] )
- Adds an element to the end of a sequence in a table.
- table
-
- The table.
- value
-
- The value to add.
- index
-
- The index for the value to be inserted.
- return-value
-
- The value that was passed in.
The add() function adds an element to a sequence in a table.
If an index is not supplied, it is assumed to be #table+1 and the value is appended to the end of the table.
The add() function returns the object being added. You can ignore this return value if you don't need it.
A stack can be easily emulated by using add(s,v) to push and v=deli(s) to pop.
Examples
t = {1, 5}
add(t, 7) -- t = {1, 5, 7}
add(t, 9) -- t = {1, 5, 7, 9}
add(t, 3, 2) -- t = {1, 3, 5, 7, 9}
-- used to create and keep track of all players
players = {}
function new_player(pad_index, name)
return add(players, {pad_index, name})
end