|
Laurent,Don't replace table.remove(f, k), it's ok.table.remove is meant to be used with tables with "valid indices." Your top-level table has "holes" and non-integer keys, whereas your deeper tables have "valid indices."The other issue is that you are modifying tables while you are iterating over them. The combination of table.remove for a "list" iterated over with ipairs, and setting to nil with pairs seems to be ok, but not other combinations.ipairs, remove with table.removepairs, remove with t[k] = nileOn Mon, Jun 20, 2016 at 6:07 PM, Laurent FAILLIE <l_faillie@yahoo.com> wrote:Le Mardi 21 juin 2016 0h00, Doug Currie <doug.currie@gmail.com> a écrit :
> You should change>> table.remove(tbl, i)>> to>> tbl[i] = nilBut why can I do it for the top level table and not inner ones ?xtbl = {
[1] = { "A" },
[2.5] = { "A", "B" },
[.5] = { "A" },
[5] = { "A", "B", "C" }
}
function dump(tbl)
for i,f in pairs(tbl) do
print(i, #f, f)
for k,v in ipairs(f) do
print(' ---', k, v)
end
end
end
function remove(tbl, func)
for i, f in pairs( tbl ) do
for k,v in ipairs(f) do
if v == func then
f[k] = nil
-- table.remove(f, k)
end
end
if #f == 0 then
print('** remove', i)
tbl[i] = nil
-- table.remove(tbl, i)
end
end
end
dump(xtbl)
remove(xtbl, 'A')
print("Resultat")
dump(xtbl)1 1 table: 0x7bf4a8
--- 1 A
5 3 table: 0x7c00b0
--- 1 A
--- 2 B
--- 3 C
2.5 2 table: 0x7bf3c8
--- 1 A
--- 2 B
0.5 1 table: 0x7bf1d0
--- 1 A
** remove 1
** remove 0.5
Resultat
5 3 table: 0x7c00b0
2.5 2 table: 0x7bf3c8As you can see, I can't walk against inner ones :(