Comparing variable values in looping IF statement

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
JohnOlek
Newbie
Posts: 5
Joined: Wed May 14, 2014 4:20 pm

Comparing variable values in looping IF statement

Post by JohnOlek » Wed May 14, 2014 6:43 pm

I have a script where I would like to compare the value of two variables and branch based on whether or not they are equivalent.

There is an array variable that has values of Var[1] through Var[n]

I want to write something like the following:

Code: Select all

let>counter=1
while>counter<=n
    if>%comparisonVariable%=Var[%counter%]
    // Code if values are the same
    else
    // Code if values are different
    endif
endwhile>
So the first loop would check to see if Var[1]=comparisonVariable, second loop would check if Var[2]=comparisonVariable, etc.

When I tried to implement this, it seemed like Var[%counter%] was being treated like a string.

Am I using incorrect syntax here?

I tried %Var[%counter%]% but that didn't work, and I also tried using complex expression syntax, but I couldn't get it to work.

I deleted my original code because I found a different solution to the issue I was trying to address, so the above code is just based on my memory. I would definitely like to understand the proper syntax for this for future projects, however.

Thanks in advance!

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Comparing variable values in looping IF statement

Post by JRL » Wed May 14, 2014 6:57 pm

Most Macro Scheduler functions do not understand a variable in the format text_%number% which is the format for Macro Scheduler arrayed variables. The fix is to use the Let> function which does understand the arrayed variable format.

Code: Select all

let>counter=1
while>counter<=n
    Let>VarValue=Var[%counter%]
    if>%comparisonVariable%=%VarValue%
    // Code if values are the same
    else
    // Code if values are different
    endif
endwhile>

JohnOlek
Newbie
Posts: 5
Joined: Wed May 14, 2014 4:20 pm

Re: Comparing variable values in looping IF statement

Post by JohnOlek » Wed May 14, 2014 7:21 pm

I could've sworn I actually tried that and it didn't work, but I just tested again and it worked great. Thanks!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts