how to delete all elements in a Lua table?

后端 未结 5 840
长发绾君心
长发绾君心 2021-02-04 00:06

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 w

5条回答
  •  梦谈多话
    2021-02-04 01:03

    #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.

提交回复
热议问题