lua-users home
lua-l archive

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


Hello,
I wanted to do it in a C function, I think I could do it like Wim
Couwenberg's first version, using 'lua_next(...)'.

It is no problem if the contents are collected later by the GC, but the
table should become empty, so I can fill it with new data without
collisions. (That is the resulting table should only have the data i put in
;)

However this 'non-defined index' problem begins to worry me. What kind of
index is this, and how do you prevent it? Moreover, would it be possible to
port Adam D. Moss' version to C?

----- Original Message ----- 
From: "Wim Couwenberg" <w.couwenberg@chello.nl>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Sunday, May 18, 2003 12:10 AM
Subject: Re: Clearing tables


> > 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
>
>