[FR] expr ternary operator support

ch's icon

Hi,
It would be great if expr/vexpr could support the ternary operator.
Is that something that you could consider adding?
so that we could write things like that :
expr ($f1 < 1) ? $f2 : $f3

Ben Bracken's icon

Here are some ways to do ternary operations with jstrigger and gen (no tilde):

Max Patch
Copy patch and select New From Clipboard in Max.

ch's icon

sure there are workarounds, but it's missing in expr and it would be really handy to have it there.

Roman Thilenius's icon


in theory it is dangerous to use arithmetics for bools from float input, but i believe really only in theory.

expr (($f1 < 1) ? $f2 : $f3 ) --> expr (($f1 < 1)*$f2 + ($f1 >= 1)*$f3)

and yes it is longer and not as good to read.

whereever you dont need to combine it with more stuff, you could make yourself an abstraction with regexp where you transform your (($f1 < 1) ? $f2 : $f3 ) into the above form by scripting an expr.

unneccessary to add that you could also transform it into [if ($f1 < 1) then $f2 else $f3], or, in the concrete case, use split & 2 trigger.

which is actually a good idea, which i will maybe steal right now.

the elvis operator has left the patch.

-110

ch's icon

expr (($f1 < 1)*$f2 + ($f1 >= 1)*$f3)

yes, I know about it too: I've used that trick many many times in the past, and that's because I was using it today again that I decided to request the ternary operator! :)
again; I know there are alternatives (note that it applies to all expr's operators really!) but it really seems to me that the ternary operator should be supported by expr and vexpr.

Roman Thilenius's icon


when i think about it, scripting the content of the patch is only required when you want to have true int type output when all input numbers are ints.

and regexp is only needed when you want to include a syntax check.

otherwise you could - for the usual conditional statement - just write something like
[110.ternary input>3?5:7] or, even simpler, [iif 3 5 7] and parse the arguments.