table.remove()

Type Function
Library table.*
Return value (varies)
Revision Release 2024.3703
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.

table.remove() may affect performance because it has to shuffle all of the elements in the array (following the one being removed) down by one. Thus, it should not be used in time-critical sections of code.

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