Switch including operators

Hey, I would like to swap a hex code based on if the number is
. >0
. =0
. <0
Tried using the switch function, but it removes the operators on execution. Why is that? Can’t operators be used within the switch function?

I already tried to parse the 0s (zeros) as numbers, did not help.
Also tried several options like without the toString function that I need to use in a different field to make it work, but now I simply wanted to put that into the switch function.

Here is the full function I tried:

I thought about putting that initial full function into a separate module (set variables), which would consume more operations, but make it cleaner … BUT that wouldn’t even help in the case if operators cannot be used within the switch …

Hi @marvin_ae,

Your problem looks very interesting, but it is currently a little bit abstract :exploding_head:.
So could you give us some examples of input value and the corresponding expected results, and of course a description of your treatment, it will help a lot (at least me).

BR,

PBI

2 Likes

Here is an example of data representing “46. High potentials”:

126, 129, 129, 129, 0, 1, 0, 0, 0, 0, 0, 0, 0

What the switch expression basically does is do 126-129 (first value minus second value of string). This is already working in a different variable/output where I simply need that value. Now with the described problem, I simply take the same function and use it as the switch’s expression. (So that shouldn’t be the problem in my eyes …)

@Philippe_Billet @vendy

Regarding your issues:
The switch function removing operators: As explained, this is because switch doesn’t interpret comparison operators in case labels.
Parsing zeros as numbers and attempting various options without success: It suggests that converting your logical comparisons into a format compatible with switch is necessary, as shown above.
Moving the logic to a separate module for cleanliness is a good idea, especially if the logic becomes more complex or needs to be reused in multiple places. However, the core of your solution will likely involve converting your condition into a switch-compatible format, regardless of where the code resides.

3 Likes

@IrvinHansen could you elaborate on a “switch-compatible format”?

Or simply an alternative way to use the SWITCH …

I think that you don’t use correctly the switch, according to the documentation. You must use a value for example +1, 0 ou -1, it is not possible to use > 0, = 0, < 0.
Unfortunately for you, it seems that there is no sign function in Make, and it is a true problem.
So that leaves nested ifs:
Let’s say that X is the result of your computation

if X > 0
then "your value if X > 0"
else if X==0
        then "your value if X == 0"
        else "your value if X < 0"
        end_if
end_if

That’s why it is interesting to compute X first, it is more readable ; the translation in Make is quite easy).

Hope it helps.

3 Likes

Thank you for the confirmation!
And indeed, that would be a handy feature …
But for now, I need to tackle it with nested ifs. Not to problematic if it does not escalate too much.

Thanks again and this if course works. :slight_smile:

3 Likes