Passed Variables

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
BobDDstryr
Junior Coder
Posts: 38
Joined: Fri Oct 22, 2010 2:47 am

Passed Variables

Post by BobDDstryr » Thu Dec 02, 2010 5:13 am

Hey guys,

I'm currently working on a script that is passed a variable through the command line. However, if no variable is provided, I'd like to prompt for it.

Is there any way to do a check if a variable exists? Or to set a default value for a variable if its not passed into the script?

The closest I've found online is to read the length of the value of the variable, and if its 0, then it doesn't exist, but this seems clunky.

Along those lines - I'm blanking on the name, but is there a "break" character?

for example I have:
let>car=10
message>%car%
will print 10. but lets say that I wanted to actually print %car%, and not the value of car - is there a way to tell it that those %s aren't denoting a variable? something like '%car'%? If so, would a comparison to see if %car% = '%car'% work?

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

Re: Passed Variables

Post by jpuziano » Thu Dec 02, 2010 7:15 am

Hi BobDDstryr,
BobDDstryr wrote:for example I have:
let>car=10
message>%car%
will print 10. but lets say that I wanted to actually print %car%, and not the value of car - is there a way to tell it that those %s aren't denoting a variable? something like '%car'%?
This is in response to only the above section of your post...

When you said "lets say that I wanted to actually print %car%"... I am going to assume that you want to print that (display that) via a Message> or MessageModal> command... Run the following script:

Code: Select all

Let>car=10

//this displays 10
MDL>%car%

//this also displays 10
MDL>{"%car%"}

Let>literal={"car"}
//even this displays 10
MDL>%literal%

Let>car={"car"}
//by reassigning the variable car to contain the literal string c-a-r , we can actually display %car%
MDL>%car%

//and it also works when mixed with other text as well
MDL>the %car% won't start
This happens because "variables keep resolving".

Here's an open challenge to all...

Code: Select all

Let>car=10
MDL>?????
Starting with the above two lines, replace ????? with whatever you can come up with to make it display only following literal string:

%car%

If that's not possible, then add whatever other lines you need to between the above two lines but do not redefine the variable i.e. the variable car must stay equal to 10.

Possible or not possible? :twisted:
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: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Dec 02, 2010 7:18 am

Is there any way to do a check if a variable exists? Or to set a default value for a variable if its not passed into the script?
Use the Assigned function:

Code: Select all

Assigned>parm,haveParm
If>haveParm=FALSE
  Let>parm=default_value
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

bnc1
Pro Scripter
Posts: 127
Joined: Sun Jul 31, 2005 5:10 pm

Post by bnc1 » Thu Dec 02, 2010 10:05 am

Code: Select all

Let>car=10

let>pct=%
let>newvar=%pct%"car"%pct%
stringreplace>newvar,",,newvar

mdl>newvar
:D :D :D

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

Post by Marcus Tettmar » Thu Dec 02, 2010 10:13 am

Let>car=10
MessageModal>{"%"+"car"+"%"}
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 » Sun Dec 05, 2010 3:47 am

bnc1 wrote:

Code: Select all

Let>car=10

let>pct=%
let>newvar=%pct%"car"%pct%
stringreplace>newvar,",,newvar

mdl>newvar
:D :D :D
Very clever, that does work... thanks bnc1!
mtettmar wrote:Let>car=10
MessageModal>{"%"+"car"+"%"}
Ahh yes... that's what I was looking for. This is no doubt the best way to do this, using "literal strings" inside a {complex expression} and making sure to use the concatenation operator + to join the various literal strings together.

Code: Select all

Let>car=10
MessageModal>{"%"car"%"}
//error: Line xxx: c not appropriate
I tried the above and it produced the above error, the c in car was not appropriate... we need that concatenation operator or a valid function at that spot or it complains... error checking... great.

So then I tried the following to see what would happen:

Code: Select all

Let>car=10
MessageModal>{"%""%"}
//displays: %"%
At first I thought this was a problem with the error checking... after processing the first "%" inside the {complex expression}, why wasn't it telling me " not appropriate... similar to the previous example? And why was it displaying %"% instead of %%?

And then I believe I figured out why. Inside the complex expression, when it hits the first " it then looks for next matching " but when it hits "" it considers that an escaped " meaning we want to display a double-quote character there so it continues on until it hits the last " and it now has its literal string as this: %""% and again, the "" in the middle is escaped so it displays: %"%

So then I tried this:

Code: Select all

MessageModal>{"%"%"}
//displays:Line: 187 % not appropriate
That makes sense, the first literal string it found there was: "%" and after that, no concatenation operator or function, just % which is not appropriate.

Code: Select all

Let>car=10
MessageModal>{"%"}{"car"}{"%"}
//displays: } not appropriate
I also note that the above will not work... it seems if you use a complex expression in a MessageModal> command, that single complex expression must be the entire message. I don't think this is mentioned anywhere in the Help file but it does seem to be the case. Is this true Marcus and are all my assumptions above correct?

Side note: One day, an example of concatenating literal strings inside a complex expression would be nice to see in the Complex Expression Examples topic in the Help file but until then... we have the forums.

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

BobDDstryr
Junior Coder
Posts: 38
Joined: Fri Oct 22, 2010 2:47 am

Post by BobDDstryr » Fri Dec 10, 2010 2:35 am

Thanks guys! Assigned is just what I needed, but reading the rest was interesting, and is good to know.

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