table.insert()

Type Function
Library table.*
Return value none
Revision 2017.3179
Keywords table, array
See also table.remove()

Overview

Inserts a given value into a table. If a position is given, inserts the value before the element currently at that position.

Syntax

table.insert( t, value )

table.insert( t, pos, value )
t (required)

Table. A table to which the new value will be added. When a table has an element inserted both the size of the array and the element indices are updated.

pos (optional)

Number. The index of the table at which the new element will be inserted. The default value is the length of the table + 1 so that table.insert(t,x) inserts x at the end of table t. Note that it is faster to use the length operator: t[#t + 1] = x.

value (required)

The new value to assign to be inserted into the table.

Example

local t = { 1, "three", 4, "five" }
print ( table.concat(t, ", ") )  --> 1, three, 4, five
table.insert( t, 2, "two" )  
print ( table.concat(t, ", ") )  --> 1, two, three, 4, five