[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5.2 Length Operator and tables (bug?)
- From: Petite Abeille <petite.abeille@...>
- Date: Thu, 19 Apr 2012 00:42:29 +0200
On Apr 19, 2012, at 12:11 AM, Roberto Ierusalimschy wrote:
> This one does not follow the definition in the manual (where, for
> instance, non-number keys do not interfere with sequences).
yeah, that was intentional in that case… i.e. is it really a pure sequence or not...
To be more literal with the manual, one could reshuffle things a bit, something along these lines:
local function is_seq( aTable )
for aKey in pairs( aTable ) do
if type( aKey ) == 'number' and aKey > 0 and aKey == math.floor( aKey ) then
if aTable[ math.max( aKey - 1, 1 ) ] == nil then
return false
end
end
end
return true
end
local aTable = { 1, 2, 3, a = true, b = false }
print( is_seq( aTable ), #aTable )
> true 3
> But the trick 'aKey - 1' is cute :)
:P
- References:
- Re: Lua 5.2 Length Operator and tables (bug?), Joseph Manning
- Re: Lua 5.2 Length Operator and tables (bug?), Coda Highland
- Re: Lua 5.2 Length Operator and tables (bug?), Dirk Laurie
- Re: Lua 5.2 Length Operator and tables (bug?), Coda Highland
- Re: Lua 5.2 Length Operator and tables (bug?), joao lobato
- Re: Lua 5.2 Length Operator and tables (bug?), Coda Highland
- Re: Lua 5.2 Length Operator and tables (bug?), joao lobato
- Re: Lua 5.2 Length Operator and tables (bug?), Eduardo Ochs
- Re: Lua 5.2 Length Operator and tables (bug?), Roberto Ierusalimschy
- Re: Lua 5.2 Length Operator and tables (bug?), Petite Abeille
- Re: Lua 5.2 Length Operator and tables (bug?), Roberto Ierusalimschy