Question

How do I delete all elements inside a Lua table? I don't want to do:

t = {}
table.insert(t, 1)
t = {}  -- this assigns a new pointer to t

I want to retain the same pointer to t, but delete all elements within t.

I tried:

t = {}
table.insert(t, 1)
for i,v in ipairs(t) do table.remove(t, i) end

Is the above valid? Or is something else needed?

Was it helpful?

Solution

for k in pairs (t) do
    t [k] = nil
end

Will also work - you may have difficulty with ipairs if the table isn't used as an array throughout.

OTHER TIPS

Table elements insert and remove performance compare

The Table size count 10000000

[1] while and rawset

while #t ~= 0 do rawset(t, #t, nil) end

spent time = 0.677220

[2] next and rawset

for k in next, t do rawset(t, k, nil) end

spent time = 0.344533

[3] ipairs and rawset

for i, v in ipairs(t) do t[i] = nil end

spent time = 0.012450

[4] for, rawset

count = #t
for i=0, count do t[i]=nil end

spent time = 0.009308

Table elemnets insert

[1] table insert function

for i=0, 10000000 do    table.insert(t, i, i) end

spent time = 1.0590489

[2] use #t

for i=0, 10000000 do    t[#t+1] = i end

spent time = 0.703731

[3] for, rawset

for i=0, 10000000 do rawset(t, i, i) end

spent time = 0.100010

result.

Fastest remove : 4

Fastest insert : 3

easiest and most performant:

for k,v in pairs(tab) do tab[k]=nil end

What you suggest isn't usable: table.remove shifts the remaining elements to close the hole, and thus messes up the table traversal. See the description for the next function for more info

For a faster version that ignores the __pairs metamethod:

local next = next
local k = next(tab)
while k ~= nil do
  tab[k] = nil
  k = next(tab, k)
end

EDIT: As @siffiejoe mentions in the comments, this can be simplified back into a for loop by replacing the pairs call with its default return value for tables: the next method and the table itself. Additionally, to avoid all metamethods, use the rawset method for table index assignment:

for k in next, tab do rawset(tab, k, nil) end

#table is the table size and so if t = {1,2,3} then #t = 3

So you can use this code to remove the elements

while #t ~= 0 do rawset(t, #t, nil) end

You will go through the table and remove each element and you get an empty table in the end.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top