Bug? Feature? Double variable replacement?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
djs
Junior Coder
Posts: 47
Joined: Sun Apr 29, 2012 4:21 pm

Bug? Feature? Double variable replacement?

Post by djs » Sun May 27, 2012 3:00 pm

Imagine this, everyonce in a while, strange things happen in code. As part of my macro, I had to convert a list of names from all upper case to upper/lower case. No big deal, right???

I used a loop in a spreadsheet to read the data out of the columns. The loop counter was a variable of 'K'. There were 16 columns in the spreadsheet.

I started out with a simple code segment like this:

Code: Select all

Let>K=16
Let>FName=KENNETH
Let>name=%FName%
MidStr>FName,1,1,FName
Length>name,nLength
MidStr>name,2,nLength,name
LowerCase>name,name
Concat>FName,name
Let>LName=SMITH
Let>name=%LName%
MidStr>LName,1,1,LName
Length>name,nLength
MidStr>name,2,nLength,name
LowerCase>name,name
Concat>LName,name

MDL>%FName% %LName%
What happened in this case is FName would become K, which is correct. When displayed though, K = 16, so the first name would be 16enneth rather than Kenneth. It was double decoding the variable.

I wonder how many other times I've ran into this problem. It was kind of cool once I understood what was happening, but it caused some problems in my script, on a very random basis!!!! (Only when one of the names started with K).

I did a simple change to the code and it works for what I need.
Instead of using:

Code: Select all

Concat>FName,name
I simply used:

Code: Select all

Let>FName=%FName%%name%
This worked as expected. Yes, I did this for both the first and last names.

Dan

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 May 28, 2012 6:36 am

Have you tried using VAREXPLICIT?

You can tell Macro Scheduler only to resolve variables that are contained within % symbols and to leave anything else as literals by setting the VAREXPLICIT variable to 1:

Code: Select all

Let>VAREXPLICIT=1

Let>K=16
Let>FName=KENNETH
Let>name=%FName%
MidStr>%FName%,1,1,FName
Length>%name%,nLength
MidStr>%name%,2,%nLength%,name
LowerCase>%name%,name
Concat>%FName%,%name%

Let>LName=SMITH
Let>name=%LName%
MidStr>%LName%,1,1,LName
Length>%name%,nLength
MidStr>%name%,2,%nLength%,name
LowerCase>%name%,name
Concat>%LName%,%name%

MDL>%FName% %LName%
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

djs
Junior Coder
Posts: 47
Joined: Sun Apr 29, 2012 4:21 pm

Post by djs » Mon May 28, 2012 3:26 pm

Ahh, you are right, great wise man. VAREXPLICIT would work, but I would still be editing the other several thousand lines of code to make this work :)

For a lot of reasons, this seems like a bug to me. It was easy to work around. For me, it was one of those things to think about when a macro works 80% of the time but fails in strange ways intermittently.

Sortof a "Note to Self"
If things don't work as expected, make sure there isn't a variable assigned to the character BEFORE spending hours blaming other parts of the code!!!

Dan

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 May 28, 2012 4:09 pm

I would also suggest that you don't use single letters as variables.

And variables in Macro Scheduler are Case Sensitive, so I tend use a lower case letter followed by an uppercase letter followed by lower case letters like vTest1, qOrderNumber, cCount, lLoop, etc. You already did this with "nLength". And VAREXPLICIT is not needed.

I modified your code to follow those rules:

Code: Select all

Let>vK=16
Let>vFname=KENNETH
Let>vName=%vFname%
MidStr>vFname,1,1,vFname
Length>vName,nLength
MidStr>vName,2,nLength,vName
LowerCase>vName,vName
Concat>vFname,vName
Let>vLname=SMITH
Let>vName=%vLname%
MidStr>vLname,1,1,vLname
Length>vName,nLength
MidStr>vName,2,nLength,vName
LowerCase>vName,vName
Concat>vLname,vName

MDL>%vFname% %vLname%
So you may be able to modify existing scripts using RegEx to change Case, add leading lowercase letter, etc. But, as a general rule, if your scripts are working now, they probably don't need to be modified, just incorporate new naming conventions in your new scripts.

PS - your initial posting mentioned changing to Upper/Lower Case. I first thought you meant from KENNETH to kenneth, or from smith to SMITH. But your script is showing converting from Upper Case to what I have always referred to as CamelCase. Remember Macro Scheduler does have functions UpperCase and LowerCase (which I see that you did use)
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

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