[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Lua next version
- From: "greegree" <greegrees@...>
- Date: Mon, 22 Jun 2009 11:12:07 +0800
Anyway, length operator for tables is so odd and buggy. I'd rather use
ipairs than # operation. Check the example below.
There is an array: arr = {1, 2, 3, 4, 5}. If carelessly set arr[3]=nil
somewhere, #arr is still 5. But if arr[4] is nil, #arr == 3.
See the interpreter console below.
> a = {1, 2, [4] = 4, [5] = 5}
> =#a
2
> a[3] = 3
> =#a
5
> a[3] = nil
> =#a
5
> for k,v in ipairs(a) do print(k,v) end
1 1
2 2
> for k=1, #a do print(k, a[k]) end
1 1
2 2
3 nil
4 4
5 5
> a[5] = nil
> =#a
4
> a[4] = nil
> =#a
2
> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br
> [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Peter Cawley
> Sent: Monday, June 22, 2009 2:53 AM
> To: Lua list
> Subject: Re: Lua next version
>
> On Sun, Jun 21, 2009 at 7:38 PM, bb<bblochl@arcor.de> wrote:
> > Is`nt it possible to tackle that problem in the new version?
>
> First you need to see it as a problem, which I for one do not. The
> length operator for tables (IMO) returns the length of an array, where
> an array is a table whose integers keys are continuous from 1 to N. An
> array with holes in it is thus not an array, as its integer keys are
> non-continuous, and hence the length operator is less well defined.
> For non-arrays, the length operator plus one gives a free integer key.
>
> If you were to "fix" this "problem", how would you define the length
> operator for tables?
> 1) Total number of keys in the table?
> 2) Total number of integer keys in the table?
> 3) Biggest integer key in the table? (and for tables with no integer
keys?)
> 4) Smallest integer key in the table which is followed by a nil? (and
> for tables with no integer keys?)
> 5) Something else?