[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Behaviour of table.remove()
- From: Duncan Cross <duncan.cross@...>
- Date: Thu, 29 Oct 2009 12:57:36 +0000
2009/10/29 Марк Гуревич <mark_gurevich@mail.ru>:
> Hello, everyone!
> Could somebody explain me the behaviour of table.remove() in the following code:
>
> function f(t)
> print(t[1], t[2])
> table.remove(t,1)
> print(t[1], t[2])
> table.remove(t,1)
> print(t[1], t[2])
> end
> f{1,nil,3}
> f{1,nil,3,4}
>
> The output of f{1,nil,3} is
> 1 nil
> nil 3
> nil 3
>
> and the output of f{1,nil,3,4} is
> 1 nil
> nil 3
> 3 4
>
> Why does the second call of table.remove() leave nil in t for f{1,nil,3} and remove nil from t for f{1,nil,3,4} ?
>
In Lua, a table is not considered to be "array-like" if it has nils in
the middle of it. Features like table.insert(), table.remove(),
table.sort() and the # length operator require that the table be
"array-like", or they have undefined, inconsistent behaviour.
(A common way to get around this is to find some other "placeholder"
value instead of nil, such as the boolean value false.)
-Duncan