lua-users home
lua-l archive

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


There is no 'clear' in the table library, so normally one would simply
reassign a new empty table. There are situations where this is
undesirable, e.g. more references to the table are lying around and
you want them to point to the cleared table; there may be values in
string-keyed fields etc.

The following monkey-patch can be used unless you have fiddled around
with metamethods.

table.clear = function(tbl)
   local n=#tbl
   table.move(tbl,n+1,2*n,1)
end

It relies on the proviso that there are not supposed to be any
non-nils at integer keys past the table's length.