[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Testing for a sequence (was Re: Lua 5.2 Length Operator and tables (bug?)
- From: Dirk Laurie <dirk.laurie@...>
- Date: Thu, 19 Apr 2012 20:17:02 +0200
>
> local function IsSequence( aTable )
> for aKey in pairs( aTable ) do
> if not( type( aKey ) == 'number' and aKey > 0 and aKey == math.floor( aKey ) and aTable[ math.max( aKey - 1, 1 ) ] ~= nil ) then
> return false
> end
> end
>
> return true
> end
>
The definition says "the set of its positive numeric keys is equal to
{1..n} for some integer n." So that test returns false too easily.
E.g.
> a=table.pack(1,2,3,4)
> =IsSequence(a)
false
Actually, if the purpose is to define when #t and the table library
work, the definition should say: "the set of its positive integer
numeric keys is empty or equal to {1..n} for some positive integer n."