lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Nick Gammon wrote:

I want to delete all elements from a table (any sort of
keys, not just numeric).

Is this OK?

t = { a = 1, b = 2, c = 3, 4 = 4}

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

Yes.

It *seems* to work however I seem to recall that you
aren't supposed  to change keys during a table traversal

From the manual:

# The behavior of next is undefined if, during the
# traversal, you assign any value to a non-existent field in
# the table.  You may however modify existing fields.  In
# particular, you may clear existing fields.

--
Aaron