Dynamic variable

I found this topic and I would like to see if someone can break it down for me, why it's done this way:

So there's this
%%Variable%%myVar%Variable%myVarIndex%%%

and I would like to understand the structure of it. Where is the % inside the yellow circle coming from?

image

I can see how the first % could be related to the last %, the same with the 3rd % being related to the second to last, and the %Variable%Local__myVarIndex% being the variable that will give me a number (so this would be the dynamic variable, I guess).
Still not getting why there's another % (in the yellow circle)?

Following the default way of setting a variable (%Variable%Local__myVar%), this is what I would see as "logical"
image

I'm trying to understand this so I can add more variables, if needed.
Even if I use this as a template for 2 variables, 1 of them being dynamic, I would like to understand the structure if I have 2 or more (maybe this is not something I will do very often, if ever, but I'd still like to understand it).

1 Like

EXAMPLE Dynamic Variable.kmmacros (3.0 KB)

But this is all very convoluted and exactly what Keyboard Maestro dictionaries were designed to deal with in a much more direct way:

KM Dictionaries can directly use a dynamic value i.e. a variable for the Key:

To extract the value stored in this Dictionary entry we would write either of the below without any extra steps:

%Dictionary[Red,3]%

Or

%Dictionary[Red,%Variable%Index%]%

EXAMPLE Dictionary.kmmacros (3.5 KB)

Apart from being easier to use than dynamically named variables, dictionaries also create less clutter.

To do the same thing with variables you would end up storing lots of variables. For example variables called:

Red1
Red2
Red3
Red4
Red5
etc etc

But with a Dictionary you would just store a single Dictionary called:

Red

2 Likes

Furthermore, a dictionary isn't restricted to numerical indices. I recently uploaded a macro called MoveMouseToWord() which stores data like this:

Dictionary[WordLocations,AnyWord]=55,111,100,55,5

The index in the dictionary is any single word or phrase like "Click to Continue" and the data it stores is the X,Y,W,H,F location where the word was found.

1 Like

I finally took the time (and courage) to come back to this topic and see if I could fully understand this.

Take away (so far): it's easy to use the dynamic variables by just doubling the % that don't belong to the variable that's supposed to be dynamic, so:
%%Variable%%Red%Variable%Index%%%
I wonder how that goes when we have variables inside variables inside variables? I hope I will never need that, but...

I can't, no matter how much I try (no exaggeration, I've been trying for the last hour) fully understand the advantage of dictionaries. Here's why:

1 - I can't understand how I would use the dictionary to change the index value of a variable such as: %Variable%Local__[%Variable%Index%]%

2 - If I want to use indexes on 500 individual words inside a variable, it's pretty easy to copy and paste those words into a variable. How to set that with a dictionary? It seems that I need to run a Set Dictionary Value for each value (in this case, 500), so to me, that's not practical at all compared to using variables.

I'm probably missing something here, for sure, and as much as I think I'm pretty smart to understand these things, but right now I'm feeling pretty stupid haha

Also, your examples seem to be a bit confusing. The way you created your macro where it replaces a variable with "Apple" would be easily achieved with a normal Set Variable action, that's why I can't fully understand the advantages of using a Dictionary vs a Variable.

For my personal workflows, the main goal so far has been to being able to access variable indexes dynamically and that, with dictionaries, seems a bit confusing and counterproductive. But again, I'm probably missing something here, so if you don't mind, can you please enlighten me?

For example this is what I mean:
Dynamic Variable Macro (v11.0.1)

Dynamic Variable.kmmacros (22 KB)

Keyboard Maestro Export

In which case you don't need a dynamically named Variable (which is what the original topic you asked about was discussing).

If you want to use a Variable to access a particular place in a Variable array you can just quote the Variable that holds the number of that place directly without having to use any filters etc.

Retrieve from position in Variable.kmmacros (4.1 KB)

Note that when you use the Variable that holds the position number, you write it directly (as if it was in a Calculation field) without any %s

This would work:
%Variable%LOCAL__List[LOCAL__Index]%

This would not work:
%Variable%LOCAL__List[%Variable%LOCAL__Index%]%

Since KM11 it is also possible to save data to a position in a Variable array using either a number or a Variable that holds a number (in the example below the two magenta Actions are setting new items to particular positions in the array):

Set to position in Variable array KM11.kmmacros (5.7 KB)

2 Likes