To %% or not to %%... That is the question...

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

To %% or not to %%... That is the question...

Post by Phil Pendlebury » Sat Nov 22, 2008 5:11 pm

This has been a bit of blur for me since the start but I think I should sort it out.

I use a screen capture line as an example:

Say I have read the variable deftolerance from an ini file and so deftolerance = 50

My line is:

FindImagePos>%SCRIPT_DIR%\graphics\%mutegraf%,%SCRIPT_DIR%\screen\export.bmp,%deftolerance%,1,mx,my,mute

or it could be:

FindImagePos>%SCRIPT_DIR%\graphics\%mutegraf%,%SCRIPT_DIR%\screen\export.bmp,deftolerance,1,mx,my,mute

Both of the above work fine. In this case the variable will never be touched or changed while the script is running.

Which is best and if possible why?

This is not a request for support so not urgent. Maybe more of a help for me and others and a topic for discussion.
Phil Pendlebury - Linktree

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 » Sat Nov 22, 2008 6:58 pm

I tend to always use %variable% format with the %.

The two main reasons that I do this are:
1. Log files show the actual value of the variable vs. the variable name. This is extremely useful when troubleshooting.
2. Easy to identify variables in strings and formulas.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Sun Nov 23, 2008 2:42 am

Bob Hansen wrote:I tend to always use %variable% format with the %.

The two main reasons that I do this are:
1. Log files show the actual value of the variable vs. the variable name. This is extremely useful when troubleshooting.
2. Easy to identify variables in strings and formulas.
Hi Bob,

That is what I have been doing but I just wondered if it was wrong in any way.

Thanks for that.
Phil Pendlebury - Linktree

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

Post by JRL » Sun Nov 23, 2008 6:02 am

When resolving a variable surrounding the variable in percents is never "wrong".

When assigning a variable surrounding the variable in percents is almost always wrong. One exception is when assigning an arrayed variable, the variable used as the array counter will be surrounded by percents. See this thread for a discussion about assigning variables with percents.

Parameters are the values passed to or set by a function. If the function has multiple parameters, they are separated by commas. If a parameter contains a variable and the variable is the entire parameter no percents are needed. The exception is when the system variable "VarExplicit" is set to 1.
Help for VarExplicit: wrote:Macro Scheduler will look at any parameter to see if it is a variable. If a variable with that name exists, the value assigned to that variable is used, otherwise just that literal value is used. Setting VAREXPLICIT to 1 stops this behaviour and tells Macro Scheduler only to resolve a variable if it is contained within % symbols.
For example:

Code: Select all

Let>VarExplicit=0
Let>WinX=100
Let>WinY=100
MouseMove>WinX,WinY
//Or
MouseMove>%WinX%,%WinY%
//Either way is correct since VarExplicit=0
If a parameter contains a variable and the parameter contains even one character that is not a part of the variable name, then the variable must be surrounded by percents.
For example:

Code: Select all

Let>VarExplicit=0
Let>FileName1=Test.txt
Let>FileName2=Final.txt
CopyFile>C:\temp\%FileName1%,C:\Data\%FileName2%
To summarize, Percents are needed around a variable when:
1) There are characters in a parameter that are not part of the variable.
2) VarExplicit is set to 1.

Percents are NOT needed around a variable when:
1) Assigning a value to the variable.
2) The variable is the parameter.

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

Post by Marcus Tettmar » Sun Nov 23, 2008 1:56 pm

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
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Sun Nov 23, 2008 4:02 pm

mtettmar wrote:Here's an article I wrote in July:

http://www.mjtnet.com/blog/2008/07/04/u ... -variable/
Ahhh there you go. :-)
Phil Pendlebury - Linktree

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