lua-users home
lua-l archive

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


On Tue, Oct 03, 2006 at 08:37:31PM +1000, Nick Gammon wrote:
> From: Nick Gammon <nick@gammon.com.au>
> To: Lua list <lua@bazar2.conectiva.com.br>
> Subject: Re: Is this Lua table an array?
> Date: Tue, 3 Oct 2006 20:37:31 +1000
> X-Mailer: Apple Mail (2.752.2)
> 
> 
> 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.

If you mean array as in the definition used by the # operator

  For a regular array, with non-nil values from 1 to a given n, its
  length is exactly that n

And you want to make sure the table meets those conditions (why? :-),
then doing pairs() and checking each key is >=1 and <= #t, and that you
see exactly #t of them, should work. I don't think that pairs() will
return a key if the value is nil.

Cheers,
Sam