Question

How do i clear all elements in an associative table? I know of :

for key in pairs(t) do
    t[key] = nil
end

But that only clears the normal table.

Was it helpful?

Solution

for key in pairs(t) do
    t[key] = nil
end

would clear any table entirely. I think you might be confusing it with

for key in ipairs(t) do
    t[key] = nil
end

this would clear the table of keys 1 till n where n is the first key for which t[n+1] == nil.

Example:

t = {
 [1] = 2,
 [2] = 3,
 [4] = 2,
 a = 1
}

First method would clear it, second method would leave keys 4 and a in it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top