get Key Name from a value (Dict)
Hi!
wondering if dict allows to get the "key" name "from a value" it stores.
es: "food" : "pizza"
"get food" give me "pizza", of course
I'd like to have "xxx pizza" to get me "food".
Possible?
looked into manual, but seems there's not this feature...
No. You would have to do it the long way. getkeys message to list all the keys. Then search through the list of keys to find the required value. Or perhaps Dict is not the best tool for the job. Are you doing this in js/Javascript?
Thanks Simon, not yet, I plan to move there soon though...
Thanks again! :-)
Just remember that keys are unique, but values are not, so your example
"get food" give me "pizza", of course
I'd like to have "xxx pizza" to get me "food".
is not really possible with a dict, not because of missing "lookup by value" but because the cardinality is (many[key]-to-one[value]), not (one[key]-to-many[values]).
thanks Nick!
The same value may be attached to many keys, dicts may be nested or mixed with arrays...
However some solutions are possible. Here is a draft based on JavaScript - very sketchy, but may be a good base for more developed construction.
Basically: it's parsing provided dictionary to produce for every value (also for values stored in arrays, nested, etc.) unique name based on location of the value in the dictionary (so the name may be later used to husk a particular key from the dict). Use "filter" message to restrict output to the value(s) in which you are interested (this is, I think, something what you are looking for):
Yaniki you are a legend! Nice patch!
That's amazing Yaniki,
thanks a lot! ;-)
Thank you guys! I am very pleased that you like this draft.
In various forum threads you can find patches for decomposing dictionaries without using JavaScript - perhaps you could build a similar mechanism without scripting. But I recommend checking these patches with different dictionaries - it often turns out that these patches cannot properly decode nested dictionaries, they cannot cope with e.g. arrays containing objects, nested dicts, arrays of dicts, , etc. I think such a properly working patch for decomposing dictionaries (without JS) is buildable, but would require some work.
Anyway: here is another incarnation of my script. In this version I also added the ability to filter keys (not just values) - and this is something that is easy to do without scripting, but I found it worth having a complete mechanism at hand. So we have filtering by key and filtering by value now - both filters works simultaneously - it means that using both filters at the same time can retrieve e.g. only keys with specific values.
I also added an extra "bang" after parsing is finished (it might come in handy if parsing - for example with filters - doesn't get any results, and you want to have a signal that parsing is over). Btw. two filters can be confusing - remember to clear filter which you don't want to use (but before that you used and set some keys or values to husk).
So appreciated! and thank you so much for the further info ;-)