Assigning a variable with percents around it

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

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

Assigning a variable with percents around it

Post by JRL » Fri Jul 27, 2007 10:37 pm

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

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Sat Jul 28, 2007 2:20 am

Whoa that's sneaky :shock: Great explanation though :)

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sat Jul 28, 2007 7:17 am

Only use percents when calling for the value of a variable.
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.

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?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Re: Assigning a variable with percents around it

Post by jpuziano » Sat Jul 28, 2007 7:20 am

Hi JRL,

Thanks for that very good explanation of what can happen.
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.
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.

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.
JRL wrote:Perhaps an editor enhancement suggestion would be to throw an alarm whenever someone tries to use percents in a variable assignment.
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:I would object to making it an illegal action because I think it could be useful if used properly.
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.

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 - :-)

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Sat Jul 28, 2007 7:27 am

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.
Let>array_%k%=somevalue

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?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Mon Jul 30, 2007 4:21 pm

mtettmar wrote:Let>array_%k%=somevalue

This feature is very necessary and is here to stay!
:oops: Oops... the mechanism to address array variables uses percent signs on the left... forgot about that (it must be summer) sorry Marcus.

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 - :-)

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Jul 30, 2007 4:24 pm

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?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Mon Jul 30, 2007 4:46 pm

Hi Marcus,
mtettmar wrote:when you see % symbols around something it means "give me the VALUE of ...".
That's a good mantra. I'll add it to my list of MS coding mantras, the top one of which has always been...
  • "variables keep resolving"
...which is a short form of something you said years ago:

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 - :-)

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Mon Jul 30, 2007 5:01 pm

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:
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.
But, I ALWAYS use %variable% so that I can see the value of the variable in the Log files.

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!

utz
Newbie
Posts: 18
Joined: Sat Aug 11, 2007 8:33 pm

Post by utz » Sun Aug 19, 2007 2:20 am

Hello everyone,

NOW I understand why my script was breaking. What a nightmare! After reading this I did a quick search on my script for "let>%" and changed the few instances of it there were... fixed it right away. It makes perfect sense now! I just wanted to thank all the contributors.

Utz

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