lua-users home
lua-l archive

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


On Thu, Jun 26, 2003 at 08:56:19PM +0400, Grisha wrote:
> I'v found another useful function, IMHO worthy including in the library:
> 
> function table.find(t, value)
>     for k,v in t do
>         if v == value then return k end
>     end
> end

What if you only want to go through string keys? Or you want more than one
result?

function table.gfind(t, value)
    function gf(t, st)
        local v
        repeat
            st, v=next(t, st)
            if v==value then
                return st
            end
        until not st
    end

    return gf, t
end

function table.find(t, value)
    for k in table.gfind(t, value)
        return k
    end
end

Or maybe table.find should return all entries? Maybe we should allow
matching functions instead of matching just by value? Even this isn't
generic enough...

-- 
Tuomo