lua-users home
lua-l archive

Re: Clearing tables

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


> This is probably more efficient in lua 5.0 (untested), and at
> the very least rather simpler:
>
> function zap(table)
> for k in table do
> table[k] = nil
> end
> end
I see, as long as no _new_ keys are inserted into a table during an
iteration, this is OK. It looks like field [k] is removed in the loop but
it is not, its value is merely set to nil. This is probably a (the?) reason
why tables don't shrink until assigning to a non-existing key?
Saves some trouble I guess. You can never be sure how "next" will respond
for a "non-defined" index though (success or failure will depend on the
"history" of the table so to speak) but you shouldn't use "next" for
non-defined keys anyway...
Bye,
Wim

AltStyle によって変換されたページ (->オリジナル) /