Hey all! So, I'm just wondering if there's a simple way of clearing the values of a table. I believe you can use a for loop to go through each value, and remove it, but is there a simpler way? Any advice? Thanks!
If you just need to replace a table with an empty one, you can simply do that:
local a = {1, 2, 3} print(#a) -- 3 a = {} print(#a) -- 0
However, this won't always be what you want:
local a = {1, 2, 3} local b = a print(#b) -- 3 a = {} print(#b) -- 3 (still)
In general, you should avoid doing this sort of thing -- saving references to things you take in is bad because it can be confusing (like in the above).
You have no choice but to change the actual value, which you need to do with a loop. Luckily, it's not hard at all to remove all of the values from a table:
for index in pairs(tab) tab[index] = nil end
local table = {1, 2346, 6236246, "stuff", true} table = {}
You can just overwrite the table with a blank table :)