I have a key-value pairs list in a text file on my desktop with this format:
Britain__kleinschmidti
western Europe to western Germany and northern Switzerland__rhenanus
Attached macro allows me to find the value for a key. But I do not need the 'Prompt With List' action because the key is already defined in the variable 'TypeSubspeciesOnly'.
But I cannot figure out how to find the value for a key, without having to run the 'Prompt With List' action. In the attached instance, the key is 'kleinschmidti' and I need to find the value 'Britain' without having to run 'Prompt With List'.
Here's an alternative using regular expressions, with the caveat that it will fail badly if "__" isn't always the key/match divider. It will also do unexpected things if you have duplicate keys, but that would sort of defeat the purpose of having keys :).
This isn't as robust as @ComplexPoint's solution, but if (like me) you're not a programmer, it may be a bit easier to understand what it's doing—though regular expressions can be incredibly obtuse. In this case, though, we're using a fairly basic one to find the split point (__) between the value/key pairs, then capturing the key that matches the value passed in in the variable.
Annoyingly unperceptive or slow to understand; stupid; insensitive. Also, of a remark, action, etc.: exhibiting dullness, stupidity or insensitivity; clumsy, unsubtle. Formerly also: †rough, unpolished;
Regular expressions have many limitations, but we shouldn't be too unkind to them, or expect too much of them
As for the JavaScript for Automation action, I hope its meaning is not too obscure:
JSON.stringify() returns a JSON string in exchange for a given list or dictionary.
String.split("\n") returns a [String]Array from a given String.
Array.reduce starts with a seed value, and allows every item in an Array to contribute a little information to the further growth of that seed.
But what I meant to convey was that they can be really difficult to understand, especially if you don't work with them a lot and just see one in text somewhere.
It's not that I think it's obscure at all—I think it's a very cool solution, and I've bookmarked it.
But as a non-coder, even knowing what JSON.stringify does, I would never be able to write the brief bit of code that you wrote without some sort of constant access to the manual to understand things such as "why the heck does 'dot dot dotdict' do?" and "what does 'null, 2' do and why is it needed?" That makes it a non-viable solution for me, unless I can use code that someone else has already written, because writing it from scratch is beyond my skill set.
Note that I'm not asking you to explain those things (though I am curious), as I'm sure you could do so clearly. But as much as I'd love to learn JS, it's very unlikely to happen beyond the basic knowledge I have now, given my age, free time, and All The Other Things That Must Get Done. Ergo, I fall back to regular expressions, because I know them already and I can usually force a solution through one, even if it is a round hole for my square problem :).
If I had my life to do over, I would definitely have focused more on coding than I did.
Never too late, I'm approaching the traditional biblical sell-by date myself, but there are still plenty of languages (human and formal) to learn
Treated realistically (as the approximate search engines that they are) LLMs are quite quick and useful for looking up things like the:
null, 2 (indented pretty-printing) arguments to JSON.stringify, and
the three ('spread syntax') dots which bring all the existing key-value pairs into the expanded dictionary which we are creating.
(They are naturally bad at assembling working code to match a specification, but much better at re-composing, from manuals and discussions, explanations of syntax).
I just did an approximate search, with Google's Gemini, using the prompt:
In the following code,
1. what are the null, 2 arguments to JSON.stringify, and
2. what is the meaning of the three dots before dict in ...dict ?
JavaScript code follows:
(...)