lua-users home
lua-l archive

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


On 29/06/2011 18.15, Dirk Feytons wrote:
Hi,

The reference manual clearly describes what can and cannot be done to
a table during traversal: modifying and clearing an existing field is
OK, assigning to a non-existing field is bad.
But what happens if I clear an existing field and then assign to it
again during one iteration? In pseudocode:

for k,v in pairs(t) do
   t[k] = nil
   if (<some condition>) then
     t[k] =<some other value>
   end
end

Is this allowed? We've seen some intermittent errors in our code that
I suspect are coming from a pattern like described above.


According to the letter of the manual, it is not allowed.
When you clear a field that field becomes non-existent. So if you assign to it afterward, you violate the rule.

I cannot say, though, if the current implementation is tolerant about that and if the failures you describe actually depends on that.

Try to rewrite the code in order to clear a field only if it must not be reassigned.

----------------------------
for k,v in pairs(t) do
   if (<some condition>) then
     t[k] =<some other value>
   else
     t[k] = nil
   end
end
----------------------------

cheers
-- Lorenzo