Assigning a variable with percents around it
Moderators: JRL, Dorian (MJT support)
Assigning a variable with percents around it
I've run into this situation a couple of times and thought I would elaborate on the subject and fish for comments or criticisms. The situation is usually an accidental use of percents around a variable that is being assigned a value. This looks innocent but can cause a great deal of confusion. This is not a bug, this is as it should be and is a usable technique. It's just a little confusing, difficult to explain and difficult to diagnose when you've buried it in 1000 lines of code.
For example: in the following script what is the value of var3?
Let>var1=a
Let>var2=a
Let>%var1%=2
Let>var3=%var1%+%var2%
MessageModal>%var3%
It might at first glance appear that the value should be "2+a" But it turns out the value is actually "4".
If you step through this in the editor you won't see the values of var1 or var2 change because they did not change, they have both been set to "a". What changed was the value of the value "a"
This becomes more insidious when the variables are initialized by setting them to "nul" because "nul" is a value and can be set to a new value.
For example: in the following script what is the value of var2?
Let>var1=
Let>var2=
Let>%var1%=2
MessageModal>%var2%
If you guessed that var2 is nul you are correct. Unfortunately nul has been reset to 2 so var2 is also equal to 2.
Use of Let>VAREXPLICIT=1 can ease this situation a little in that the reporting of the variable value will be explicit. That is, you will get the value the variable is assigned rather than the value of the value that is assigned to the variable. With VAREXPLICIT set to 1 in the following script, var3 will show a value of "a+a" but "a" was still set to "2" and this could still become an issue should VAREXPLICIT get reset to 0 especially if "a" is used later and expected to have the value a.
Let>VAREXPLICIT=1
Let>var1=a
Let>var2=a
Let>%var1%=2
Let>var3=%var1%+%var2%
MessageModal>%var3%
MessageModal>%a%
Hope this makes sense and helps reduce the confusion this can cause. The bottom line is that for most situations be careful not to set a variable with percents around it. Only use percents when calling for the value of a variable.
Perhaps an editor enhancement suggestion would be to throw an alarm whenever someone tries to use percents in a variable assignment. I would object to making it an illegal action because I think it could be useful if used properly.
Thanks for listening,
Dick
For example: in the following script what is the value of var3?
Let>var1=a
Let>var2=a
Let>%var1%=2
Let>var3=%var1%+%var2%
MessageModal>%var3%
It might at first glance appear that the value should be "2+a" But it turns out the value is actually "4".
If you step through this in the editor you won't see the values of var1 or var2 change because they did not change, they have both been set to "a". What changed was the value of the value "a"
This becomes more insidious when the variables are initialized by setting them to "nul" because "nul" is a value and can be set to a new value.
For example: in the following script what is the value of var2?
Let>var1=
Let>var2=
Let>%var1%=2
MessageModal>%var2%
If you guessed that var2 is nul you are correct. Unfortunately nul has been reset to 2 so var2 is also equal to 2.
Use of Let>VAREXPLICIT=1 can ease this situation a little in that the reporting of the variable value will be explicit. That is, you will get the value the variable is assigned rather than the value of the value that is assigned to the variable. With VAREXPLICIT set to 1 in the following script, var3 will show a value of "a+a" but "a" was still set to "2" and this could still become an issue should VAREXPLICIT get reset to 0 especially if "a" is used later and expected to have the value a.
Let>VAREXPLICIT=1
Let>var1=a
Let>var2=a
Let>%var1%=2
Let>var3=%var1%+%var2%
MessageModal>%var3%
MessageModal>%a%
Hope this makes sense and helps reduce the confusion this can cause. The bottom line is that for most situations be careful not to set a variable with percents around it. Only use percents when calling for the value of a variable.
Perhaps an editor enhancement suggestion would be to throw an alarm whenever someone tries to use percents in a variable assignment. I would object to making it an illegal action because I think it could be useful if used properly.
Thanks for listening,
Dick
- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Something I'm always telling people. Rule of thumb: Use percents when referencing variables, not when creating them. Personally I don't use percents at all unless they are needed. If referencing a variable on it's own they are not needed.Only use percents when calling for the value of a variable.
Exceptions to the rule exist of course, such as assigning array type variables in a loop (Let>array_%k%=something)
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Re: Assigning a variable with percents around it
Hi JRL,
Thanks for that very good explanation of what can happen.
As has been often mentioned here on the forums, choosing long descriptive variable names goes a long way towards eliminating any unintentional "variables keep resolving" problems. By doing that, I almost never have to resort to using Let>VAREXPLICIT=1 and don't often need percents around variables. If I can make the code work without using VAREXPLICIT or percents, I prefer it that way. Its easier to understand and therefore to change in the future and has the added plus of being compatible with older versions of Macro Scheduler.
However, I'd be surprised if anyone out there actually had some code that intentionally used this propery and I'd be even more surprised if that code was in some way superior (i.e. shorter, faster, easier to understand) than the equivalent code written in a straightforward manner. If anyone has or can come up with such an example, please share, who knows where it may lead.
Thanks again JRL for shedding some light on a dark corner...
Thanks for that very good explanation of what can happen.
Yup, I never use percents on the left of the equals sign, only on the right and even then, as little as possible. The example you provided about the value of null being "reset to 2" is ample proof of how this could really make things confusing.JRL wrote:The bottom line is that for most situations be careful not to set a variable with percents around it. Only use percents when calling for the value of a variable.
As has been often mentioned here on the forums, choosing long descriptive variable names goes a long way towards eliminating any unintentional "variables keep resolving" problems. By doing that, I almost never have to resort to using Let>VAREXPLICIT=1 and don't often need percents around variables. If I can make the code work without using VAREXPLICIT or percents, I prefer it that way. Its easier to understand and therefore to change in the future and has the added plus of being compatible with older versions of Macro Scheduler.
I'd support that, but you'd almost need the warning message to pop up a full page of info explaining "why" doing that is dangerous... much like the explanation you provided in your post or... I don't think the warning would be fully understood.JRL wrote:Perhaps an editor enhancement suggestion would be to throw an alarm whenever someone tries to use percents in a variable assignment.
Well, since this has been with us for a while, my guess is it will have to stay... if only to keep existing scripts working as they do now.JRL wrote:I would object to making it an illegal action because I think it could be useful if used properly.
However, I'd be surprised if anyone out there actually had some code that intentionally used this propery and I'd be even more surprised if that code was in some way superior (i.e. shorter, faster, easier to understand) than the equivalent code written in a straightforward manner. If anyone has or can come up with such an example, please share, who knows where it may lead.
Thanks again JRL for shedding some light on a dark corner...
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -

- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Let>array_%k%=somevalueHowever, I'd be surprised if anyone out there actually had some code that intentionally used this propery and I'd be even more surprised if that code was in some way superior (i.e. shorter, faster, easier to understand) than the equivalent code written in a straightforward manner. If anyone has or can come up with such an example, please share, who knows where it may lead.
This feature is very necessary and is here to stay!
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
mtettmar wrote:Let>array_%k%=somevalue
This feature is very necessary and is here to stay!

So this required feature will be staying and... I'd say the idea to pop up a warning msg when a percent is detected on the left of the equals sign looses its value as this would get annoying for users who write a lot of code using array variables.
Ah well, it was still a great explanation of what could happen JRL... and it should help users avoid that pitfall in their own scripts.
Take care
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -

- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Just remember when you see % symbols around something it means "give me the VALUE of ...". If you say that every time you won't go wrong. So if you put it on the left you will assign something to the VALUE of the variable, not the variable itself. % symbols allow you to refer to the VALUE of something. Repeat after me .....
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Hi Marcus,
everything is seen as a variable unless you turn VAR_EXPLICIT on. It keeps resolving
Thanks and take care
That's a good mantra. I'll add it to my list of MS coding mantras, the top one of which has always been...mtettmar wrote:when you see % symbols around something it means "give me the VALUE of ...".
- "variables keep resolving"
everything is seen as a variable unless you turn VAR_EXPLICIT on. It keeps resolving
Thanks and take care
jpuziano
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -
Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post -

- Bob Hansen
- Automation Wizard
- Posts: 2475
- Joined: Tue Sep 24, 2002 3:47 am
- Location: Salem, New Hampshire, US
- Contact:
It's funny how we all have developed different work habits.
In an earlier posting, Marcus wrote that he doesn't use them at all:
If I don't use the % around the variable, then the Logs only show the variable name, but knowing the value of the variable at time of execution is usually more important to me.
There are many ways to accomplish tasks, but the best one is usually the one that works for YOU. But when the only tool you have is a hammer, then every problem looks like a nail. Need to keep adding tools ......
Good discussion Dick, thanks for starting the thread with your explanations.
In an earlier posting, Marcus wrote that he doesn't use them at all:
But, I ALWAYS use %variable% so that I can see the value of the variable in the Log files.Something I'm always telling people. Rule of thumb: Use percents when referencing variables, not when creating them. Personally I don't use percents at all unless they are needed. If referencing a variable on it's own they are not needed.
If I don't use the % around the variable, then the Logs only show the variable name, but knowing the value of the variable at time of execution is usually more important to me.
There are many ways to accomplish tasks, but the best one is usually the one that works for YOU. But when the only tool you have is a hammer, then every problem looks like a nail. Need to keep adding tools ......
Good discussion Dick, thanks for starting the thread with your explanations.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!
Bob
A humble man and PROUD of it!