[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua, LuaJIT2 and differences with the length operator
- From: Leo Razoumov <slonik.az@...>
- Date: Tue, 4 Jan 2011 17:18:03 -0500
On Tue, Jan 4, 2011 at 16:09, Norbert Kiesel <nkiesel@tbdnetworks.com> wrote:
> On Tue, 2011-01-04 at 16:16 -0200, Roberto Ierusalimschy wrote:
>> But if you really want to check, just to make sure, this is how you
>> can do your job. I assume you know at least that the value is a
>> table. Change as needed to your specific circumstances:
>>
>> function noholes (t)
>> local n = #t
>> local i = 0
>> for k in pairs(t) do
>> if type(k) == 'number' then
>> if math.floor(k) == k and 1 <= k and k <= n then
>> i = i + 1
>> else
>> return false
>> end
>> end
>> end
>> return (i == n)
>> end
>>
>>
>
> Should that not better be
>
> if type(k) == 'number' and math.floor(k) == k and 1 <= k and k <= n then
> i = i + 1
> else
> return false
> end
>
> i.e. always return false if we find a non-number key, given the "if t[1]
> is nil, #t can be zero" caveat in the definition of #t?
>
> </nk>
>
I would rather prefer:
if type(k) == 'number' and math.floor(k) == k then
if 1 <= k and k <= n then
i = i + 1
else
return false
end
end
This way, table's hash part as well as numeric keys with fractional
part like 3.33 (which belong to hash part) are excluded from testing.
--Leo--
- References:
- Re: Lua, LuaJIT2 and differences with the length operator, Leo Razoumov
- Re: Lua, LuaJIT2 and differences with the length operator, Dirk Laurie
- Re: Lua, LuaJIT2 and differences with the length operator, Leo Razoumov
- Re: Lua, LuaJIT2 and differences with the length operator, Roberto Ierusalimschy
- Re: Lua, LuaJIT2 and differences with the length operator, Norbert Kiesel