table.remove

Removes an item from a numerically-keyed table

Prototype

val = table.remove (t, pos)

Description

Removes the element at position 'pos' from the table, returning the value of the removed element.

If 'pos' is omitted it defaults to the end of the table (n), thus removing the last element.

t = { "the", "quick", "brown", "fox" }
print (table.remove (t, 3))
table.foreachi (t, print)

 -->
 
brown
1 the
2 quick
3 fox
The Lua authors recommend using this method for removing from the end of a table nowadays:

t [#t] = nil  -- remove last entry
Note that for other keys (eg. strings) you simply assign nil to the element to remove it. For example:

t = {}
t.foo = "bar"  -- add item
t.foo = nil    -- remove item

Lua functions

Topics