how to delete all elements in a Lua table?

后端 未结 5 858
长发绾君心
长发绾君心 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 00:38

    easiest and most performant:

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

    What you suggest isn't usable: table.remove shifts the remaining elements to close the hole, and thus messes up the table traversal. See the description for the next function for more info

提交回复
热议问题