Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Comparing variables to find the highest value (TWINE 2, HARLOWE)

How do I set up an if statement to check if a variable is the highest out of four separate variables.

I assume I can say:

(if: $variable1 > $variable2 and $variable3 and $variable4)[run script A]
(if: $variable2 > $variable1 and $variable3 and $variable4)[run script B]
(if: $variable3 > $variable2 and $variable1 and $variable4)[run script C]
(if: $variable4 > $variable2 and $variable3 and $variable1)[run script D]

But what if there is a joint highest - how can I give an order of priority so that only one script runs no matter what.
For example, I want script a to take priority over scripts B, C, and D - so if A and C are tied, it is script A that gets run

Comments

  • First a small clarifications...
    Unless the $variable2, $variable3 and $variable4 variables in your first (if:) macro example contain Boolean values, that is not how you use the and operator to test multiple conditions/expressions.
    It should look similar to the following:
    (if: $variable1 > $variable2 and $variable1 > $variable3 and $variable1 > $variable4)[run script A]
    

    One method to do what you asks is to first determine the maximum value contained within your variables, and then you can compare that max value to each of your variables (in the order of importance) to see which one equals it.
    note: You can use an (else:) macro to catch the last of the variable checks, because if it is not one of the other variables then it must be the last one.
    (set: $variable1 to 10, $variable2 to 20, $variable3 to 5, $variable4 to 20)
     
    (set: $max to (max: $variable1, $variable2, $variable3, $variable4))
    max: $max
     
    (if: $max is $variable1)[Do variable1 thing]
    (else-if: $max is $variable2)[Do variable2 thing]
    (else-if: $max is $variable3)[Do variable3 thing]
    (else:)[Do variable4 thing]
    
    ... If you want variable 4 thing to be more important that variable 2 thing then just change the order of the variables.
    (if: $max is $variable4)[Do variable4 thing]
    (else-if: $max is $variable2)[Do variable2 thing]
    (else-if: $max is $variable1)[Do variable1 thing]
    (else:)[Do variable3 thing]
    
Sign In or Register to comment.