Set Variable to Calculation Using Dynamic Variables in the Calculation


I am trying to get a sum of a varying number of values - there can be anywhere from one to eight. The values are already stored in variables:


So I created a Repeat block that will be repeating n times.
I have a Counter variable running from 1 to n
I have a variable TimeRunningTotal that will be used to accumulate the sum of the times.

During each loop, I need:
TimeRunningTotal = TimeRunningTotal + TimeLineTotal(x)
"(x)" being the current value of Counter

I had thought a "Set Variable to Calculation" step looking something like this would work:
TimeRunningTotal = TimeRunningTotal + TimeLineTotal%Variable%Counter%

So in, say, the 2nd loop, when Counter = 2 I would get:
TimeRunningTotal = TimeRunningTotal + TimeLineTotal2

BUT, the Set Variable to Calculation step doesn't like the

How can I calculate with a variable that needs to be referred to dynamically like this?


  • eric

Hey Eric,

Look at the For-Each loop in this macro:

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


Hello, again! Thanks.
I don't see a Set variable there using a calculation with a dynamic variable.
I see a simple Set with + 1
Maybe I'm missing the obvious...

  • eric

Got this to work thusly:


For each found item a new variable foundText_(varIndex) is created.


There's a couple of other ways to do this.

  1. Use your 8 variables and set the unused ones to 0 -- then you can always sum all 8 in a single operation and still get the correct value.
  2. Don't use 8 variables, use an array with (up to) 8 elements. You'll still have to loop through the elements to add them, but it's a lot easier than double-escaping/filtering etc. You can read more about KM's arrays in the Using Variables section of the manual.