lua-users home
lua-l archive

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


Hi!

2010/12/28 Enrico Colombini <erix@erix.it>:
> On 28/12/2010 9.25, Dirk Laurie wrote:
>>
>> How does one iterate over all the keys of a table _except_
>> the ones that ipairs would reach?
>
> (slow and untested):
>
> local skip = {}
> for i, _ in ipairs(t) do
>    skip[i] = true
> end
> for k, v in pairs(t) do
>    if not skip[k] do
>        -- something
>    end
> end

FWIW, I tried to wrap this in an iterator like function:

function hpairs( t )
    local skip = {}
    for i, _ in ipairs( t ) do
        skip[i] = true
    end

    local k, v = next( t )
    while skip[ k ] do
        k, v = next( t, k )
    end

    return function()
        local j, w = k, v
        k, v = next( t, k )
        while skip[ k ] do
            k, v = next( t, k )
        end
        return j, w
    end
end

Regards,
Matthias