lua-users home
lua-l archive

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


=== FAQ ===

Hi Ando,

How to find the key for a table value? Since Lua doesn't keep this information in the table, you must either search the table, or build another table with the reverse mapping of values to keys. The latter is more economical if you need to find the keys for many values.

http://lua-users.org/lists/lua-l/2003-06/msg00559.html

You could even add a __newindex event to the metatable for t so that the reverse table is built incrementally as key/value pairs are added to t. IIRC, Rici posted an example a little while ago.

- Peter


Ando Sonenblick wrote:
t = {
    a = function() ... end,
    b = function() ... end
    }

I know need to get an indexed function name from the table.  For index 1, I
need back "a", index 2, "b" and so on.

This can be done with foreach, but is there no way in lua to directly access
a specific item?

Such as:

print(t[1].key) ????