If a<n<b processing structure for numeric comparisons

If you have created a unique MLServeCmd that performs a unique task, please post it here so others can learn from them.
User avatar
bhiga
Expert
Expert
Posts: 854
Joined: Tue Mar 08, 2005 10:28 pm
Location: San Jose, CA
Contact:

If a<n<b processing structure for numeric comparisons

Postby bhiga » Fri Mar 18, 2005 12:27 am

Description
Sometimes you may want to perform a command if a particular variable is within a certain range. For example, you might want to show a "Save energy" button only when there were between 3 and 6 pieces of equipment in the On state. Okay, so this is a weird example...
In my case, I have an overlay with 9 buttons on them, and if I only need 6, then I want to clear the 3 I don't need and display the 6 I do.

Dependencies
This structure utilitizes MLConditional and MLMath.

Command structure
If (myVar <= maxValue) And (myVar >= minValue) Then ...

Code: Select all

MLServeCmd.Macro|
SetVariable|testValue~{{myVar}}
MLConditional|IsEqual##{{testValue}}##maxValue##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##maxValue - 1##MLMath|SUBTRACT~testValue~1##!
.
.
MLConditional|IsEqual##{{testValue}}##minValue##Command1##ElseCommand1!
MLConditional|IsEqual##{{testValue}}##minValue##Command2##ElseCommand2!
.
.
MLConditional|IsEqual##{{testValue}}##minValue##CommandN##ElseCommandN


Of course you don't have to populate all the Else commands.

Sample for "If (myVar >= 2) and (myVar <= 5) Then Set Variable Result to CarPoolOK Else Set Variable Result to NoCarPool"

Code: Select all

MLServeCmd.Macro|
SetVariable|testValue~{{myVar}}
MLConditional|IsEqual##{{testValue}}##5##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##4##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##3##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##2##SetVariable|Result~CarPoolOK##SetVariable|Result~NoCarPool


So what's really going on in there?
MLServeCmd.Macro|
Yes, it's a macro.

SetVariable|testValue~{{myVar}}
First, we copy {{myVar}}'s value to a temporary variable. If we don't do this, we'll destroy the original value of {{myVar}} and potentially confuse ourselves up later.
If you really don't need to preserve the value of {{myVar}} you can skip this step, but I highly recommend leaving it in for consistency.


MLConditional|IsEqual##{{testValue}}##5##MLMath|SUBTRACT~testValue~1##!
Now we check if testValue is equal to the maximum value, which was 5. If it is, then we decrement it by one. You'll see why shortly.

MLConditional|IsEqual##{{testValue}}##4##MLMath|SUBTRACT~testValue~1##!
Now we check if testValue is equal to one less than the maximum value, which is 4. Note that if testValue started as 5 (the previous condition was true), then it's already been set to 4, which magically makes this condition true too.

MLConditional|IsEqual##{{testValue}}##3##MLMath|SUBTRACT~testValue~1##!
We continue to check values in the range, essentially looking to see where we "catch" the right value. Once we "catch" the right value, we
"hold on to" it by decrementing it. As soon as one comparison results true, all the comparisons that follow result true as well because we're making testValue "follow" the path of the comparisons.


MLConditional|IsEqual##{{testValue}}##2##SetVariable|Result~CarPoolOK##SetVariable|Result~NoCarPool
At the minimum value, we have "collected" all values from the maximum value down to the minimum value, so now we execute whatever command we wanted to.
If we have multiple commands, we do the same comparison again.
We do not decrement testVal here, because it's unnecessary.
Besides, if we do, we'll "fall into" whatever processing we're doing for minimum value - 1.



If you need to process groups of commands, say A, B and C if the value is 3, A and B if the value is 2, and only A if the value is 1, add another MLConditional before the one that performs the subtraction.

Here is a slightly-more-complex example illustrating per-value and else processing.
Let's assume you have a five "Additional passengers" status buttons, referencing variables {{passenger2}} through {{passenger5}} and you want to display the status of each.

Code: Select all

MLServeCmd.Macro|
SetVariable|testValue~{{myVar}}
MLConditional|IsEqual##{{testValue}}##5##SetVariable|passenger5~Present##SetVariable|passenger5~Not present!
MLConditional|IsEqual##{{testValue}}##5##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##4##SetVariable|passenger4~Present##SetVariable|passenger4~Not present!
MLConditional|IsEqual##{{testValue}}##4##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##3##SetVariable|passenger3~Present##SetVariable|passenger3~Not present!
MLConditional|IsEqual##{{testValue}}##3##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##2##SetVariable|passenger2~Present##SetVariable|passenger2~Not present!
MLConditional|IsEqual##{{testValue}}##2##SetVariable|Result~CarPoolOK##SetVariable|Result~NoCarPool


MLServeCmd.Macro|
Again, it's a macro 'cause macros make the world go 'round.

SetVariable|testValue~{{myVar}}
Make a copy so we don't lose the original value of myVar.
You could copy myVar to testValue, use myVar instead of testValue in the conditionals, then reset myVar to testValue at the end instead if that code reads better to you.


MLConditional|IsEqual##{{testValue}}##5##SetVariable|passenger5~Present##SetVariable|passenger5~Not present!
If we have 5 passengers, indicate Passenger 5 as Present, otherwise indicate Passenger 5 as Not present.

MLConditional|IsEqual##{{testValue}}##5##MLMath|SUBTRACT~testValue~1##!
Decrement the testValue because if we have 5 passengers, we should do the processing for 4 passengers too, otherwise passengers 2-4 won't get listed at all.

MLConditional|IsEqual##{{testValue}}##4##SetVariable|passenger4~Present##SetVariable|passenger4~Not present!
If we have 4 passengers, indicate Passenger 4 as Present, otherwise indicate Passenger 4 as Not present.

MLConditional|IsEqual##{{testValue}}##4##MLMath|SUBTRACT~testValue~1##!
If we have 4 passengers, we also need to "trickle down" to the processing for 3 passengers. By now you should get the idea.
.
.
.
MLConditional|IsEqual##{{testValue}}##2##SetVariable|passenger2~Present##SetVariable|passenger2~Not present!
Here we set or clear the value for Passenger 2.

MLConditional|IsEqual##{{testValue}}##2##SetVariable|Result~CarPoolOK##SetVariable|Result~NoCarPool
And now we set our result value for "Is 2 <= myVar <= 5?" to note whether car pool is OK or not for that number of passengers.
Disclaimer: Car pool requirements vary from region to region. :wink:


Additional notes:
You could do the same thing by reversing the process and checking the minimum condition first then incrementing the value.

To make it a strict less-than and/or strict greater-than, just increase the max value by one and/or reduce the min value by one. You can do this manually if you're checking fixed values, or by using MLMath if you're checking against variables.

Well, I hope that helps someone because it took me a while to write!

Brandon
Last edited by bhiga on Fri Mar 18, 2005 5:28 pm, edited 1 time in total.
- Brandon
My MainLobby stuff (plug-ins, screenshots, etc)

bwally
Intermediate
Intermediate
Posts: 364
Joined: Sat Aug 14, 2004 6:23 am
Location: ontario, canada
Contact:

Postby bwally » Fri Mar 18, 2005 8:37 am

Brandon,

this is VERY useful information!
thanks so much for taking the time to put this together!

steve
another steve

Guest

Postby Guest » Fri Mar 18, 2005 7:35 pm

You're welcome!

I'm really enjoying working with MainLobby, even though I've been spending some 12+ hour days at work recently and haven't had much time to work on it.

Brandon

randy h
Senior Member
Senior Member
Posts: 230
Joined: Sat Jan 08, 2005 7:27 pm
Location: Saint Louis, MO USA
Contact:

Postby randy h » Fri Mar 18, 2005 8:16 pm

Hi Brandon,
I have been trying to find a way of checking <> for a while now and could not figure it out. Your write up was written extremely well. Well enough for even me to understand it. Your message is being saved and printed.

Thanks for taking the time to share this with us.

randy

User avatar
DavidL
Simply Incredible
Simply Incredible
Posts: 11071
Joined: Sat Feb 08, 2003 9:39 am
Location: Metamora, Michigan
Contact:

Postby DavidL » Fri Mar 18, 2005 9:30 pm

Brandon,
Excellent post. Took the liberty of adding it to the Cheat Sheet. Will post that whenever the forum attachment ability gets fixed.

Thanx.

User avatar
fungun
Intermediate
Intermediate
Posts: 298
Joined: Wed May 19, 2004 7:16 pm
Location: usa,ia
Contact:

Postby fungun » Sat Mar 19, 2005 1:28 am

Smooth you can send it to me and will put on my site. I have the older one there now.

http://starportboji.bravepages.com/homeseer/index.html

Tim

User avatar
bhiga
Expert
Expert
Posts: 854
Joined: Tue Mar 08, 2005 10:28 pm
Location: San Jose, CA
Contact:

Postby bhiga » Mon Mar 21, 2005 2:12 am

Thanks everybody!

I guess many others have the same need to check such conditions.

Check out my MainLobby page for an easier way to do comparisons (at least if you know how to form VBScript conditions, which are pretty close to plain English).

wscVBSEvaluate will evaluate a VBScript condition and set a variable to the boolean result as the string True or the string False.

So, instead of the crazy structure at the top of this thread, you could make it read a little better.

Sample for "If (myVar >= 2) and (myVar <= 5) Then Set Variable Result to CarPoolOK Else Set Variable Result to NoCarPool" using wscVBSEvaluate

Code: Select all

MLServeCmd.Macro!
wscVBSEvaluate|({{myVar}} > = 2) and ({{myVar}} <= 5)~ConditionResult!
MLConditional|IsEqual##ConditionResult##True##SetVariable|Result~CarPoolOK##SetVariable|Result~NoCarPool


Reads much easier, eh?

Remember that in VBScript strings must be enclosed in double-quotes.
Variables that contain double-quote characters will generally not work, since the double-quote in the variable will likely cause string termination problems. The alternative would've been to separate each part of the condition from each other, but that would prevent doing And and Or type conditions in the string.

One big advantage of being able to use VBScript comparions versus my previous "trickle down" or "bubble up" approach, is that you can use an "open ended" condition without needing to know both the minimum and maximum values.

For example, if you did something with temperature, you might want to set a "Freezing flag" whether the temperature is freezing or not. In this case you know what the freezing level is, but you may not know how cold the temperature range will be, so you'd have to "trickle down" to some specific value that you choose, and if the temperature went below that value your check would fail.

By using a VBScript comparison, you don't need to worry about what the minimum temperature will ever be, as you just need to check whether the temperature value is <= freezing or not.

Code: Select all

MLServeCmd.Macro!
wscVBSEvaluate|{{tempC}} <= 0~ConditionResult!
MLConditional|IsEqual##ConditionResult##True##SetVariable|IsFreezing~Yes##SetVariable|IsFreezing~No


Sorry, I don't know what freezing in Farenheit is... :oops: It never snows where I grew up. 8)


Finally... as with all my functions so far, you must take care to avoid errors - especially when dealing with double-quotes in variables. If you avoid double-quotes in variables, you should be fairly safe. Seeing a VBScript error in the middle of doing something isn't much fun.

Brandon
- Brandon

My MainLobby stuff (plug-ins, screenshots, etc)


Return to “MLServeCmd Examples”

Who is online

Users browsing this forum: No registered users and 1 guest