How do I delete all contents of a table at once?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

How do I delete all contents of a table at once?

Post by Kasperelo »

As the title says, how can I delete everything in a table at once? When I use

Code: Select all

table.remove(the table I want to delete the content of)
it starts with the last thing added and deletes it way to the start. Help?
Automatik
Citizen
Posts: 57
Joined: Sun Feb 17, 2013 7:05 pm

Re: How do I delete all contents of a table at once?

Post by Automatik »

Code: Select all

thetable = {}
User avatar
borix134
Prole
Posts: 18
Joined: Sat Aug 10, 2013 5:37 am

Re: How do I delete all contents of a table at once?

Post by borix134 »

This might be what you need:

Code: Select all

for i,v in ipairs(table) do
table.remove(table, i)
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How do I delete all contents of a table at once?

Post by Robin »

Generally, you'd want to create a new table.

Code: Select all

mytable = {}
As long as that was the only reference to the old table, it will be garbage collected.

If there is more than one reference to the table you want cleared, I think you could do:

Code: Select all

for k, v in pairs(mytable) do
    mytable[k] = nil
end
... but I'm not too sure, because I keep forgetting what works for "modifying tables while looping over them" and what doesn't, and I'm too lazy to put it to the test.

(Wow, ninja'd twice. I'm slow.)
Help us help you: attach a .love.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: How do I delete all contents of a table at once?

Post by raidho36 »

You can't delete and add keys in looping a table throuh the iterator. That is, you can, but under such conditions behavior is undefined.
User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Re: How do I delete all contents of a table at once?

Post by Kasperelo »

Automatik wrote:

Code: Select all

thetable = {}
Thanks, this worked perfectly! :awesome:



I feel like an idiot not figuring that out.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: How do I delete all contents of a table at once?

Post by T-Bone »

If you happen to only use numerical indexing, you can just do

Code: Select all

for i = #tab,1,-1 do
    tab[i] = nil
end
This is faster than creating and destroying tables if you need to do it often (like a significant number of times every frame). It's probably not something to worry about unless you actually experience slow-downs.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How do I delete all contents of a table at once?

Post by bartbes »

raidho36 wrote:You can't delete and add keys in looping a table throuh the iterator. That is, you can, but under such conditions behavior is undefined.
That is not true for next (and therefore pairs), you just can't add, but you can delete, according to the manual:
The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests