lua-users home
lua-l archive

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


Again, thx Peter...

> How to find the key for a table value?

Interesting direction... And maybe I'm not grokking it immediately, but it
doesn't seem to suit me need immediately.

Based on your description (and the code at
http://lua-users.org/lists/lua-l/2003-06/msg00559) this maps values to keys;
whereas, I want to map indeces into the table to keys...

That is, for a table of key/value pairs, give me the key (or value,
ideally), of the 'nth' item in the table....

Kinda strange this isn't available, IMHO.

> 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.

Another thing: you mention that "Lua doesn't keep this information in the
table".  It doesn¹t ?   Now THAT seems odd.  Where does it keep it?

I'm trying to not look at lua's implementation of most things, as I'd be
tempted to duplicate/modify them to suit my needs which would jeopardize
future lua compatibility, but I'm beginning to think that I might need to...

> 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.

This makes some sense and is worth looking into...    I definitely feel
there are myriad solutions to my problem; it's merely a matter of teasing
out the most appriopriate...

Ando