• Subject: Re: Table.remove() issue with the last element
• From: Laurent FAILLIE <l_faillie@...>
• Date: Mon, 20 Jun 2016 22:07:01 +0000 (UTC)

Le Mardi 21 juin 2016 0h00, Doug Currie <doug.currie@gmail.com> a écrit :

> You should change
>
>  table.remove(tbl, i)
>
> to
>
> tbl[i] = nil

But 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: 0x7bf3c8

As you can see, I can't walk against inner ones :(