table.remove()

Type Function
Library table.*
Return value Any
Revision 2017.3060
Keywords table, array
See also table.insert()

Overview

Removes from table the element at position pos, shifting down other elements to close the space, if necessary. Returns the value of the removed element.

Syntax

table.remove( t )
table.remove( t, pos )
t (required)

Table. The table from which to remove.

pos (optional)

Number. Position of the element to remove. Its default value is the length of the table, so table.remove(t) removes the last element of t.

Examples

local exampleTable = { 1,1,2,3,5,8,13 }
print( table.maxn(exampleTable) )      --> 7
print( table.remove(exampleTable,4) )  --> 3
print( table.maxn(exampleTable) )      --> 6
print( table.remove(exampleTable) )    --> 13
Empty Entire Table
local exampleTable = { 1,1,2,3,5,8,13 }
for i = #exampleTable,1,-1 do
    table.remove(exampleTable,i)
end