[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Changing tables during traversal: clearing and re-adding a field
- From: Roberto Ierusalimschy <roberto@...>
- Date: Thu, 30 Jun 2011 09:43:39 -0300
> >> >[...]
> >> >
> >> >for k,v in pairs(t) do
> >> > t[k] = nil
> >> > if (<some condition>) then
> >> > t[k] =<some other value>
> >> > end
> >> >end
> >> >
> >> >[...]
>
> What's do you mean, "not formally allowed"?
>
> "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."
> - it's from Lua 5.1.4 reference manual.
See the code: When it does t[k] =<some other value>, the field t[k] does
not exist (it was erased two lines earlier), so it is assigning to a
non-existent field, which is explicitly forbiden.
-- Roberto