How to erase or reset a table in Lua

How can I completely clear or flush a table in Lua. I want to do this in an empty table at the end.

+3
source share
3 answers

You iterate over the keys and make them zero.

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

If it is an array, then delete the values ​​using table.remove ()

+7
source

How about this way?

t = {..some non-empty table..}
...some code...
t={}
+3
source

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

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

this element will delete all table values ​​and you will not get any table:

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

Source: https://habr.com/ru/post/1791124/


All Articles