- Posts: 108
- Thank you received: 7
If Then Else
- aa6vh
- Topic Author
- Offline
- User
-
Less
More
1 year 11 months ago #3110
by aa6vh
If Then Else was created by aa6vh
Just when I think I have a handle on the Macro syntax, I run into this. Example code (in Javascript):
If (user_A == 0) {
user_a = 1;
user_b = 1;
} else {
user_a = 0;
user_b = 0;
}
user_c = "Alpha";
user_d = "Alpha";
This is the Macro code that I entered:
Macro|
If|Isequal##{{user_a}}##0##Macro|
SetVariable|user_a~1!
SetVariable|user_b~1##Macro|
SetVariable|user_a~0!
SetVariable|user_b~0!
!
SetVariable|user_c~Alpha!
SetVariable|user_d~Alpha!
//fini
Unfortunately the last two statements are not executed. Then I had an idea, and modified the Macro to add an additional "Macro" statement:
Macro|
If|Isequal##{{user_a}}##0##Macro|
SetVariable|user_a~1!
SetVariable|user_b~1##Macro|
SetVariable|user_a~0!
SetVariable|user_b~0!
!
Macro|
SetVariable|user_c~Alpha!
SetVariable|user_d~Alpha!
//fini
Now it works. I could not find the reason this worked explained anywhere, and thought this information might help other newbies like me.
If (user_A == 0) {
user_a = 1;
user_b = 1;
} else {
user_a = 0;
user_b = 0;
}
user_c = "Alpha";
user_d = "Alpha";
This is the Macro code that I entered:
Macro|
If|Isequal##{{user_a}}##0##Macro|
SetVariable|user_a~1!
SetVariable|user_b~1##Macro|
SetVariable|user_a~0!
SetVariable|user_b~0!
!
SetVariable|user_c~Alpha!
SetVariable|user_d~Alpha!
//fini
Unfortunately the last two statements are not executed. Then I had an idea, and modified the Macro to add an additional "Macro" statement:
Macro|
If|Isequal##{{user_a}}##0##Macro|
SetVariable|user_a~1!
SetVariable|user_b~1##Macro|
SetVariable|user_a~0!
SetVariable|user_b~0!
!
Macro|
SetVariable|user_c~Alpha!
SetVariable|user_d~Alpha!
//fini
Now it works. I could not find the reason this worked explained anywhere, and thought this information might help other newbies like me.
Please Log in to join the conversation.
- bowlingbeeg
- Offline
- User
-
Less
More
- Posts: 248
- Thank you received: 33
1 year 11 months ago #3111
by bowlingbeeg
Replied by bowlingbeeg on topic If Then Else
If I remember correctly once you add an inner macro then the outer macro requires an extra ! on the end of each line. It's not intuitive and really makes it difficult to add nested if then else statements because you have to change everything outside of that nested statement.
Macro|
If|Isequal##{{user_a}}##0##Macro|
SetVariable|user_a~1!
SetVariable|user_b~1##Macro|
SetVariable|user_a~0!
SetVariable|user_b~0!
!
SetVariable|user_c~Alpha!!
SetVariable|user_d~Alpha!!
//fini
Macro|
If|Isequal##{{user_a}}##0##Macro|
SetVariable|user_a~1!
SetVariable|user_b~1##Macro|
SetVariable|user_a~0!
SetVariable|user_b~0!
!
SetVariable|user_c~Alpha!!
SetVariable|user_d~Alpha!!
//fini
Please Log in to join the conversation.
- aa6vh
- Topic Author
- Offline
- User
-
Less
More
- Posts: 108
- Thank you received: 7
1 year 11 months ago #3112
by aa6vh
Replied by aa6vh on topic If Then Else
Thank you, your code also seems work properly. Now that raises the question if the way I showed will be easier to read, or will it cause further complications as the statements get more complex.
Hopefully Allonis staff will see this and weigh in.
Hopefully Allonis staff will see this and weigh in.
Please Log in to join the conversation.
- davep
- Offline
- Admin
-
Less
More
- Posts: 367
- Thank you received: 96
1 year 11 months ago #3132
by davep
Replied by davep on topic If Then Else
I'm updating the larger myServer commands document on the Support pages but I'll summerize If Then Else here.
Normal structured programming languages typically structure If statements like
If Some Condition is true Then
Do something
Else
Do something else
Way back in the day when Adobe flash ruled the world and myServer was at version 1 we came up with a server command that would test for a specified condition and act accordingly.
The rule we had to follow was that the Conditional statement had to allow for embedded macros and we needed to make sure the command line would be properly parsed and not mess with any of the embedded server commands delimiters.
The syntax we came up with at the time was to inject unique identifiers in the statement that would never appear in a normal server command.
It was decided that the Conditional statement would use pairs of punctuation characters for its delimiters. ##, $$, %%, etc.
This generated the base command structure.
Conditional|<Type Of Condition>##<condition variable>##<condition value>>##Then Commands##Else Commands
Fast forward 22 years and the conditional command has changed very little. With the exception of Conditional| changing to If| everything else remains the same.
So in myServer we have
If|<Type Of Condition>##<condition variable>##<condition value>>##Then Commands##Else Commands
Here's an example
If {{current_hour}} = 12 then
SetVariable|lunchtime~Is here
Else
SetVariable|lunchtime~Still waiting
in myServer it becomes
If|IsEqual##{{current_hour}}##12##SetVariable|lunchtime~Is here##SetVariable|lunchtime~Still waiting
Note the ELSE clause is optional.
If|IsEqual##{{current_hour}}##12##SetVariable|lunchtime~Is here
is just as valid.
If you need to embed an If statement in a If statement then that requires using different sets of delimiters
If|{{current_hour}} = 5 then
If|{{current_day}} = Friday then
SetVariable|happy_hour~engaged
We'll use ## for the outer and $$ for the inner pair.
If|IsEQ##{{current_hour}}##5##
If|IsEQ$${{current_day}}$$Friday$$SetVariable|happy_hour~engaged
Just remember the rule that you must use a different pair of delimiters for every nested if statement.
Then and Else clauses can contain macros.
A simple way to do AND tests is by combining the variables into the condition variable
If|{{current_hour}} = 5 and {{current_day}} = Friday then
SetVariable|happy_hour~engaged
Lock The Doors
Turn Off The Lights
If|IsEQ##{{current_hour}}{{current_day}}##5Friday##
Macro|
SetVariable|happy_hour~engaged!
Security|1~Mode~Away!
Lighting|5~Off
If|IsEQ##{{current_hour}}{{current_day}}##5Friday##Macro|SetVariable|happy_hour~engaged!Security|1~Mode~Away!Lighting|5~Off
Here are the types of conditions currently supported in myServer 6.
If|isRunning##<name of program>>##Then
Is the specified program currently running.
If|isEqual##<server variable>##<value>##Then
If|isEQ##<sever variable>##<value>>##Then
Is the value of the server variable equal to the test value?
If|isLTE##<sever variable>##<value>>##Then
Is the value of the server variable less than or equal to the test value?
Is|isLT##<sever variable>##<value>>##Then
Is the value of the server variable less than the test value?
If|isGTE##<sever variable>##<value>>##Then
Is the value of the server variable greater than or equal to the test value?
If|isGT##<sever variable>##<value>>##Then
Is the value of the server variable greater than the test value?
If|isNE##<sever variable>##<value>>##Then
Is the value of the server variable is not equal to the test value?
If|FileExists##<path to file##Then
Does the file exist
If|StartsWith##<sever variable>##<value>>##Then
Does the value of the server variable start with the test value?
If|Contains##<sever variable>##<value>>##Then
Does the value of the server variable contain the test value?
If|EndsWith##<sever variable>##<value>>##Then
Does the value of the server variable end with the test value?
If|isTrue##<sever variable>##Then
If|isOn##<sever variable>##Then
Does the value of the server variable test true?
True means the value is equal to 1 or True or On
If|isFalse##<sever variable>##Then
If|isOff##<sever variable>##Then
Does the value of the server variable test false?
False means the value is equal to 0 or False or Off
If|isPingable##<ip address>##Then
Can the IP Address be pinged
If|isNotPingable##<ip address>##Then
Is the IP Address NOT pingable
If|isReachable##<url>##Then
Can the URL be opened?
If|isNotReachable##<url>##Then
Can the URL not be opened?
Hopefully this helps.
Normal structured programming languages typically structure If statements like
If Some Condition is true Then
Do something
Else
Do something else
Way back in the day when Adobe flash ruled the world and myServer was at version 1 we came up with a server command that would test for a specified condition and act accordingly.
The rule we had to follow was that the Conditional statement had to allow for embedded macros and we needed to make sure the command line would be properly parsed and not mess with any of the embedded server commands delimiters.
The syntax we came up with at the time was to inject unique identifiers in the statement that would never appear in a normal server command.
It was decided that the Conditional statement would use pairs of punctuation characters for its delimiters. ##, $$, %%, etc.
This generated the base command structure.
Conditional|<Type Of Condition>##<condition variable>##<condition value>>##Then Commands##Else Commands
Fast forward 22 years and the conditional command has changed very little. With the exception of Conditional| changing to If| everything else remains the same.
So in myServer we have
If|<Type Of Condition>##<condition variable>##<condition value>>##Then Commands##Else Commands
Here's an example
If {{current_hour}} = 12 then
SetVariable|lunchtime~Is here
Else
SetVariable|lunchtime~Still waiting
in myServer it becomes
If|IsEqual##{{current_hour}}##12##SetVariable|lunchtime~Is here##SetVariable|lunchtime~Still waiting
Note the ELSE clause is optional.
If|IsEqual##{{current_hour}}##12##SetVariable|lunchtime~Is here
is just as valid.
If you need to embed an If statement in a If statement then that requires using different sets of delimiters
If|{{current_hour}} = 5 then
If|{{current_day}} = Friday then
SetVariable|happy_hour~engaged
We'll use ## for the outer and $$ for the inner pair.
If|IsEQ##{{current_hour}}##5##
If|IsEQ$${{current_day}}$$Friday$$SetVariable|happy_hour~engaged
Just remember the rule that you must use a different pair of delimiters for every nested if statement.
Then and Else clauses can contain macros.
A simple way to do AND tests is by combining the variables into the condition variable
If|{{current_hour}} = 5 and {{current_day}} = Friday then
SetVariable|happy_hour~engaged
Lock The Doors
Turn Off The Lights
If|IsEQ##{{current_hour}}{{current_day}}##5Friday##
Macro|
SetVariable|happy_hour~engaged!
Security|1~Mode~Away!
Lighting|5~Off
If|IsEQ##{{current_hour}}{{current_day}}##5Friday##Macro|SetVariable|happy_hour~engaged!Security|1~Mode~Away!Lighting|5~Off
Here are the types of conditions currently supported in myServer 6.
If|isRunning##<name of program>>##Then
Is the specified program currently running.
If|isEqual##<server variable>##<value>##Then
If|isEQ##<sever variable>##<value>>##Then
Is the value of the server variable equal to the test value?
If|isLTE##<sever variable>##<value>>##Then
Is the value of the server variable less than or equal to the test value?
Is|isLT##<sever variable>##<value>>##Then
Is the value of the server variable less than the test value?
If|isGTE##<sever variable>##<value>>##Then
Is the value of the server variable greater than or equal to the test value?
If|isGT##<sever variable>##<value>>##Then
Is the value of the server variable greater than the test value?
If|isNE##<sever variable>##<value>>##Then
Is the value of the server variable is not equal to the test value?
If|FileExists##<path to file##Then
Does the file exist
If|StartsWith##<sever variable>##<value>>##Then
Does the value of the server variable start with the test value?
If|Contains##<sever variable>##<value>>##Then
Does the value of the server variable contain the test value?
If|EndsWith##<sever variable>##<value>>##Then
Does the value of the server variable end with the test value?
If|isTrue##<sever variable>##Then
If|isOn##<sever variable>##Then
Does the value of the server variable test true?
True means the value is equal to 1 or True or On
If|isFalse##<sever variable>##Then
If|isOff##<sever variable>##Then
Does the value of the server variable test false?
False means the value is equal to 0 or False or Off
If|isPingable##<ip address>##Then
Can the IP Address be pinged
If|isNotPingable##<ip address>##Then
Is the IP Address NOT pingable
If|isReachable##<url>##Then
Can the URL be opened?
If|isNotReachable##<url>##Then
Can the URL not be opened?
Hopefully this helps.
The following user(s) said Thank You: BigShep, aa6vh
Please Log in to join the conversation.
Time to create page: 0.173 seconds