lua-users home
lua-l archive

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


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>