PICO-8 Wiki
m (→‎See also: no self-ref)
(Added optional index parameter)
Tag: Visual edit
Line 4: Line 4:
 
|tbl||The table.
 
|tbl||The table.
 
|v||The value to add.
 
|v||The value to add.
  +
|i|optional|The index for the value to be inserted.
 
}}
 
}}
 
The <code>add()</code> function appends an element to the end of a sequence in a table.
 
The <code>add()</code> function appends an element to the end of a sequence in a table.
Line 13: Line 14:
 
== Examples ==
 
== Examples ==
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
t = {1, 3, 5}
+
t = {1, 5}
add(t, 7) -- t = {1, 3, 5, 7}
+
add(t, 7) -- t = {1, 5, 7}
add(t, 9) -- t = {1, 3, 5, 7, 9}
+
add(t, 9) -- t = {1, 5, 7, 9}
  +
add(t, 3, 2) -- t = {1, 3, 5, 7, 9}
 
</syntaxhighlight>
 
</syntaxhighlight>
   
Line 24: Line 26:
 
* [[All|<code>all()</code>]]
 
* [[All|<code>all()</code>]]
 
* [[Del|<code>del()</code>]]
 
* [[Del|<code>del()</code>]]
  +
* [[Deli|<code>deli()</code>]]
 
* [[Foreach|<code>foreach()</code>]]
 
* [[Foreach|<code>foreach()</code>]]
 
* [[Pairs|<code>pairs()</code>]]
 
* [[Pairs|<code>pairs()</code>]]

Revision as of 22:39, 9 July 2020

add( tbl, v, [i] )
Adds a element to the end of a sequence in a table.
tbl
The table.

v
The value to add.

i
The index for the value to be inserted.

The add() function appends an element to the end of a sequence in a table.

This always adds the new element immediately after the current final index. It does not check for available gaps between the start and the end.

The add() function returns the object being added. You can ignore this return value, or pass it back to your own caller.

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}

See also