开发者

How to wipe or reset a table in Lua

How woul开发者_如何学JAVAd I go about completely wiping or resetting a table in Lua. I want to make it into a blank table in the end.


You iterate over the keys and make them nil.

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

If it's an array then remove values with table.remove()


What about this way?

t = {..some non-empty table..}
...some code...
t={}


this will create a new table 't' with a new pointer and delete old values:

t = {1, 2, 3}
t = {}
collectgarbage()

this one will delete all values of the table and you will end up with no table:

t = {1, 2, 3}
t = nil
collectgarbage()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜