If Var matches Dictionary "Key" then show it's text


The scenario is:

A variable "USA_state" contains one of the USA states, for example Alabama
A dictionary "USA States" has all states codes:

How to get a state code presented in the variable without using IF action 50 times?

No need for the If Then Else action at all. Just use the variable itself for the key:


%Dictionary[USA states,%Variable%E2DP_State_or_province%]%


Hey Kirill,

Dictionaries have their uses, but they're quite often more trouble than they're worth.

This lookup table is configured to go from state name to abbreviation, but you can tweak the regular expression to go the other direction if desired.


LookUp Table for U.S.A. States Names and Abbreviations v1.00.kmmacros (6.5 KB)


Hey gglick,

I have already tried this version, but it does nothing, after trying the action KM engine stops working and "Hide macro" starts processing

Chris, you are awesome as always! :clap:

1 Like

Hey Krill,

I'm glad you found a solution that works for you, but I can verify that the solution I suggested works as well:

Dictionary Variable Test.kmactions (1.6 KB)



Regarding this other issue you're apparently experiencing, without seeing the full macro or this "Hide" macro (are they the same one?) I can't say what the problem could be, but it is definitely not normal or expected behavior when using a KM dictionary. If you're still interested in getting the dictionary method working, please upload the full macro, or at least the full relevant portion if you can't share the whole one for whatever reason, and we can try to figure out what might be going wrong.

1 Like

After restarting KM editor/Mac it stopped to crash and show the "Hide" macro - not sure where the problem was.

Gabe, your version is a little bit harder to understand but looks like i got it and it also works! :+1::+1::+1:

Much respect to both of you guys @ccstone @gglick !

1 Like

Hey Kirill,

I took another stab at this just for fun.

I wanted to use only awk, but the stock version on macOS is very old and doesn't support case-insensitive searching, and I didn't want to be stuck with that limitation in the macro.

I recommend people who want to get real work done install GNU awk and sed with Homebrew or MacPorts.

You can always use Perl as I have in the macro, but sometimes an awk or sed one-liner is more compact and readable.

The base script in GNU awk would look something like this:

read -r -d '' dataStr <<'EOF'

echo "$dataStr" | gawk 'BEGIN{IGNORECASE=1}; /wIScONSIN/ { print $1 }'

The macro below supports any number of tab-separated fields in the data-string.

The user must supply a literal or regex search string and the field number they want to extract.


Download ⇢ Extract a Given Field from a Lookup Table v1.00.kmmacros (6.0 KB)

Chris, thanks a lot for one more great solution, i will definitely look into it and try this method.

The solutions provided above by you and Mr. Gabe work perfectly, one more time thanks a lot for the help, KM and you guys are making my life much easier :slight_smile: :+1: