lua-users home
lua-l archive

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


>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

If you're going to need this often, then a much better solution is to
invert the table:

function table.invert(t)
	local s={}
	for k,v in t do s[v]=k end
	return s
end

Then, instead of table.find(t,v), use s[v], where s=table.invert(t).
--lhf