PICO-8 Wiki
(helps to pay attention to the actual goal)
Tag: Source edit
(Note that index is optional and, if not supplied, turns deli into pop)
Tag: Source edit
Line 2: Line 2:
 
|name=deli
 
|name=deli
 
|shortdesc=Removes the element at the given index of a sequence in a table.
 
|shortdesc=Removes the element at the given index of a sequence in a table.
|tbl||The table.
+
|table||The table.
|i||The index for the value to be removed.
+
|index|optional|The index for the value to be removed.
 
}}
 
}}
   
Line 9: Line 9:
   
 
The deli() function returns the object being removed. This can be ignored, tested it to see if the removal was successful, or used as needed.
 
The deli() function returns the object being removed. This can be ignored, tested it to see if the removal was successful, or used as needed.
  +
  +
If the <var>index</var> is not supplied, it is assumed to be <code>#table</code>, meaning the last element in the sequence. This means a stack can be easily emulated by using <code>[[Add|add(s,v)]]</code> to push and <code>v=deli(s)</code> to pop.
  +
 
== Example ==
 
== Example ==
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">

Revision as of 10:22, 29 June 2021

deli( table, [index] )
Removes the element at the given index of a sequence in a table.
table
The table.

index
The index for the value to be removed.

The deli() function removes the value at the given index of a sequence. All subsequent values in the sequence shift down by one slot to keep the sequence contiguous.

The deli() function returns the object being removed. This can be ignored, tested it to see if the removal was successful, or used as needed.

If the index is not supplied, it is assumed to be #table, meaning the last element in the sequence. This means a stack can be easily emulated by using add(s,v) to push and v=deli(s) to pop.

Example

t = {1, 2, 4, 3, 4}
deli(t, 5)  -- t = {1, 2, 4, 3}
deli(t, 3)  -- t = {1, 2, 3}

See also