table.insert(t, [location],item,)
Insertion is done at the specified location by shifting any existing items towards the end and the table is resized.
Option |
Description |
t |
The table on which to perform the operation. |
item |
The element to insert. |
location (optional) |
The location in the table at which to insert the element. By default, set to one plus the end of the table |
You can append new elements at the end of a table array:
> t = {1,2,3}
> table.insert(t,21)
> print(Length(t))
4
> print(t[4]) // 4th entry
21
You can also insert elements at a chosen position in the table by providing the second argument location:
> table.insert(t,3,99)
> print(t[2], t[3], t[5]) // table size increased to 5
2 99 21