lua-users home
lua-l archive

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



Here's the solution that seems to be working.
Not nice, but required in the few places where I need it.

I agree with Adam that 'table.getn' could just as well be removed altogether!

-ak

--
local NEW_GETN= table.getn( {'a','b',[4]='c' } ) == 4

-- Also for 'NEW_GETN' since we can't trust its 'table.getn':
local function getn_over_holes( tbl )
    --
    local n= 0
    for k,v in tbl do
        if k~='n' then  -- skip 'n' (ancient)
            local i= tonumber(k)
            ASSUME( i, "Nonnumeric index: "..k )
            if i>n then n=i end
        end
    end
    return n
end

local getn_stop_at_holes

if NEW_GETN then
    getn_stop_at_holes=
      function( tbl )
        local n= table.getn(tbl)    -- over holes (or not..)
        for i=1,n do
            if tbl[i]==nil then   -- allow 'false'
                return i-1
            end
        end
        return n
      end
else
    getn_stop_at_holes= table.getn
end