[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Is this Lua table an array?
- From: Nick Gammon <nick@...>
- Date: Tue, 3 Oct 2006 20:37:31 +1000
On 03/10/2006, at 7:42 PM, Ashu Sharma wrote:
I am just wondering if there's a good way of finding whether a
given Lua
table is actually an array.
One way is to iterate over the table using 'pairs' and see if any
of the
keys is a non-number.
You would have to define "an array" here.
Is this an array?
t = {} --> no non-numbers
What about this?
t = { "a","b", [4] = "c" } --> has gaps
Or this?
t = { "a","b", [100000] = "c" } --> has a big gap
How about:
t = { [0] = "a" } --> no gaps, but won't be picked up by ipairs
I am inclined to think that you make your own definition of what an
array is, and write a function to detect if a particular table meets
that condition.
One way would be to traverse the table using ipairs, and again using
pairs, and see if the number of elements is the same in both cases.
Obviously that could be slow.
- Nick