-
Posted by Fogger on Thu, 29 Jan 2009 14:07:30
Is there any difference between using the below example from the book:
for key,value in pairs(tbl) do
tbl[key] = nil
endor using
tbl = {}
? -
Posted by Fogger on Thu, 29 Jan 2009 14:07:31
Is there any difference between using the below example from the book:
for key,value in pairs(tbl) do
tbl[key] = nil
endor using
tbl = {}
? -
Posted by Fogger on Thu, 29 Jan 2009 14:15:42
I now realise the above
for
loop could include extra features for selective removal.wish I could edit/delete posts. :(
-
Posted by morlando on Thu, 29 Jan 2009 16:12:41
Besides the additonal control with the
for
loop, the second method isn't actually clearing the table. It's simply creating a brand new empty table and discarding the original (the{}
brackets are technically the "create table" operators of the Lua language). Depending on how often you do something like this, you can significantly impact performance by overworking the garbage collection system. It also depends on how many entries are in the table and how much data is in each entry. Iterating through each one in the for loop may end up having a larger impact than discarding the table and letting the GC do its thing.Appendix C: Best Practices has some further discussions of these problems.
-
Posted by Fogger on Thu, 29 Jan 2009 18:37:44
Thanks Cog, I wasn't aware of that.