lua-users home
lua-l archive

Re: Lua 5.2 Length Operator and tables (bug?)

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


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

AltStyle によって変換されたページ (->オリジナル) /