lua-users home
lua-l archive

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


On 30/06/2011 14.43, Roberto Ierusalimschy wrote:
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.

I usually play it 'by the rules' (just to be doubly safe) by doing something like this (untested):

---

local changes = {}
local removals = {}

-- traverse t, request changes and/or removals
for k,v in pairs(t) do
  if (<some condition>) then
    changes[k] = <some other value> -- add to requested changes
  else
    removals[k] = true -- add to requested removals (if needed)
  end
end

-- traversal complete, apply changes
for k,v in pairs(changes) do
 t[k] = v
end

-- apply removals
for k,_ in pairs(removals) do
 t[k] = nil
end

changed = nil
removals = nil

---

...or by devising a different algorithm :-)

P.S. It could also be done with only a 'changes' table by using a special value to say 'remove' (e.g. local remove_value = {} ) and checking for this value when applying changes.

--
  Enrico