Help With Assigning a Global Value to a Dynamic Variable Name in a Subroutine

From what I've read, it should be possible to use dynamic variable names in KM. I'm not sure that's what I want, but let me explain what I'm trying to do, and hopefully someone can help.

I have a subroutine that references a certain variable name quite a few times, in statements like this:

Set variable localMathTest to localSomeNumber - permvarStoredNumberForX
Set variable localOtherTest to localOtherNumber * permvarStoredNumberForX
etc.

The bit I'm interested in being dynamic is permvarStoredNumberForX. I need to copy this particular subroutine into many macros, and the permanent storage variable needs to be unique in each version of the subroutine.

As it stands now, I have to manually edit each step that references permvarStoredNumberForX. What I'd like to do is this:

Set variable instanceWorkingNumber to '%Variable%permvarStoredNumberForX%'

Then, the various statements would look like this:

Set variable localMathTest to localSomeNumber - instanceWorkingNumber
Set variable localOtherTest to localOtherNumber * instanceWorkingNumber

When I then copy and paste the subroutine to other macros, I'd only have to edit the first Set statement; all the rest would "just work." I have read, but had no luck implementing, the dynamic names section of the Set Variable to Text help file.

So ... help? :slight_smile:

thanks;
-rob.

Hey Rob,

Here's an example:

Assign all Chinese Character Snippets into Variables - #2 by ccstone

-Chris

Thanks Chris…but I'm afraid my simple brain can't translate that into what I'm trying to accomplish. Your macro seems to be storing the results of a regex into a sequence of variables, with the names not known in advance.

I know the name I want to use; I'd just like to assign it in one spot, then reference it in many others.

While thinking about this, I came up with the "oh duh!" solution, but I'd still like to know how to do this in general as it seems useful. It'd be great to just create a variable name once, then have it magically appear where needed in the macro.

My workaround was realizing this is a subroutine, and I can pass it things and return things. So I pass it permvarStoredNumberForX, and then that turns into a localForSubroutineUse variable. When done, I pass back the localForSubroutineUse value and store it in permvarStoredNumberForX.

Works great, and I can set the variable once in the calling macro and that's that ... but still, I'd love to see a working example of how to just refer to one variable name through use of another.

thanks;
-rob.

You wanted to create a dynamically named variable in the subroutine that refers to a fixed global variable – right?

That's exactly what this bit does:

image

Never mind, I was being dense. I believe all I needed to do was this, in pseudocode...

$permVar = 6
$tempVar = $permVar

Then use $tempVar everywhere in the code, and I'd only ever have to change the first two lines when I change the variable name. As usual (sigh), I was making the problem much tougher than it had to be.

With that said, the pass/return solution with the subroutine is even better than this, as I only have to change it in one spot.

Thanks for trying to help with my non-problem! :slight_smile:

-rob.

Hey Rob,

Can't you use an instance variable?

That should be unique and not require any changing...

-Chris

The thing I was getting stuck on is that I have to read a permanent variable, and so when I first wrote that, that's just what I referred to in all my formulas. But yes, reading it to an instance variable, then using that in all the formulas, would've been the way to do it (before I figured out the subroutine return bit).

-rob.