lua-users home
lua-l archive

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


Hello,

I'm facing an issue with table.remove when I would like to remove the last element of a table.
I have the following code :

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
                table.remove(f, k)
            end
        end
        if #f == 0 then
print('** remove', i)
            table.remove(tbl, i)
        end
    end
end

dump(xtbl)
remove(xtbl, 'A')

print("Resultat")
dump(xtbl)

But the result is the following :

1    1    table: 0x59d3c8
  ---    1    A
5    3    table: 0x59e0d8
  ---    1    A
  ---    2    B
  ---    3    C
2.5    2    table: 0x59d1d0
  ---    1    A
  ---    2    B
0.5    1    table: 0x59e0b0
  ---    1    A
** remove    1
** remove    0.5
Resultat
5    2    table: 0x59e0d8
  ---    1    B
  ---    2    C
2.5    1    table: 0x59d1d0
  ---    1    B
0.5    0    table: 0x59e0b0

As you can see, the sub table '1' is correctly removed. The code said as well it removed 0.5 entry ... but I dumped it, I still have it with a 0 sized sub table.
Why ? What I've missed ?

Thanks

Laurent