[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Undefined behavior of table.remove
- From: T T <t34www@...>
- Date: Tue, 14 Dec 2010 17:07:40 +0000
On 14 December 2010 15:49, Krawtschunowski, Wladimir
<WKrawtschunowski@vectron.de> wrote:
> Hi, All.
>
> The Lua 5.1 Reference Manual describes #t to be SOME n, so t[n] is not nil and t[n+1] is nil. So the following two tables have a different result by #-Operator:
> ----
> a = {1,2,3,nil,nil,6}
> b = {}
> b[1] = 1
> b[2] = 2
> b[3] = 3
> b[6] = 6
> print (#a,#b)
> 6 3
> ----
> Ok - fine, but it seems to have some consequences: if I apply table.remove at first position of both tables - the number of keys to be shifted seems to be different.
> ----
> for k,v in pairs(a) do print(k,v) end
> 1 1
> 2 2
> 3 3
> 6 6
> for k,v in pairs(b) do print(k,v) end
> 1 1
> 2 2
> 3 3
> 6 6
> table.remove(a,1); table.remove(b,1)
> print (#a,#b)
> 2 2
> for k,v in pairs(a) do print(k,v) end
> 1 2
> 2 3
> 5 6
> for k,v in pairs(b) do print(k,v) end
> 1 2
> 2 3
> 6 6
> ----
Ouch! That's a good one.
> >From user point of view is the behaviour of the table.remove function undefined. Is it a feature or a bug?
I would call it a misfeature ;) Intended but not welcome.
Cheers,
Tomek