Dirk Laurie
2018-09-26 12:20:55 UTC
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.
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.