Find value in list Macro (v11.0.2)

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

Find value in list.kmmacros (3.3 KB)

You can use the %JSONValue% token, after converting the "__" list delimited list to JSON format:

Find value in JSON version of list.kmmacros (4.6 KB)

1 Like

But I see that in your file you have Value__Key

(rather than the more common Key__Value sequence)

So here is a variant:

Find value in JSON version of list (Variant - Key at right- value at left).kmmacros (4.3 KB)

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

Find value in list - via regex.kmmacros (3.3 KB)

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.

-rob.

Amazing thanks.

1 Like

Perhaps a bit unfair ?

OED: Obtuse from 1509-

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 :slight_smile:


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.

Array.prototype.reduce() - JavaScript | MDN

Here:

  1. The seed value is an empty dictionary {}
  2. We split each row of the [String] Array into two parts, naming them key and value.
  3. We return a slightly updated version of our dictionary, enhanced by that key:value pair.

return JSON.stringify(
    kmvar.local_SubSpecies
        .split("\n")
        .reduce(
            (dict, row) => {
                const [value, key] = row.split("__");

                return {
                    ...dict,
                    [key]: value
                }
            },
            {}
        ),
	null, 2
)

Well, I had more of the Apple Dictionary definition in mind :slight_smile: ...

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.

-rob.

1 Like

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 :slight_smile:

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:
(...)
1 Like