Passed Variables
Moderators: JRL, Dorian (MJT support)
-
- Junior Coder
- Posts: 38
- Joined: Fri Oct 22, 2010 2:47 am
Passed Variables
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?
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?
Re: Passed Variables
Hi BobDDstryr,
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:
This happens because "variables keep resolving".
Here's an open challenge to all...
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?
This is in response to only the above section of your post...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'%?
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
Here's an open challenge to all...
Code: Select all
Let>car=10
MDL>?????
%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?

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:
Use the Assigned function: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?
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Code: Select all
Let>car=10
let>pct=%
let>newvar=%pct%"car"%pct%
stringreplace>newvar,",,newvar
mdl>newvar



- Marcus Tettmar
- Site Admin
- Posts: 7395
- Joined: Thu Sep 19, 2002 3:00 pm
- Location: Dorset, UK
- Contact:
Let>car=10
MessageModal>{"%"+"car"+"%"}
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?
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar
Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?
Very clever, that does work... thanks bnc1!bnc1 wrote:Code: Select all
Let>car=10 let>pct=% let>newvar=%pct%"car"%pct% stringreplace>newvar,",,newvar mdl>newvar
![]()
![]()
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.mtettmar wrote:Let>car=10
MessageModal>{"%"+"car"+"%"}
Code: Select all
Let>car=10
MessageModal>{"%"car"%"}
//error: Line xxx: c not appropriate
So then I tried the following to see what would happen:
Code: Select all
Let>car=10
MessageModal>{"%""%"}
//displays: %"%
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
Code: Select all
Let>car=10
MessageModal>{"%"}{"car"}{"%"}
//displays: } not appropriate
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 -
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 -

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