Good patching techniques. Should I use Javascript for more?

Eplusmusic's icon

Hello, recently I have started using the if object in the place of a select object and a message box like I have been doing since I started with Max. I was just wondering, is there a neater or easier way? I think perhaps I have fallen into a bad habit.

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

ANYWHO, the point of this was to ask if any of you lovely people can make this without having a ger-zillion(sp?) patch lines and taking longer than it has to:

I am mostly interested in the part which is triggered when the input does NOT equal any of those numbers.

I have tried using the If object with this --

[if ($i1 == 8) || ($i1 == 24) ($i1 == 140) || ($i1 == 156) then 0 else 1]

But that doesn't work. Max hurling abuse at me in the Max window.

Any help is appreciated,

Iyad.

dhjdhjdhj's icon

I can't see your patch since I'm on an iPad but it seems to me you could use a [route 8 24 140 156] object and just detect output from the rightmost outlet

Roman Thilenius's icon

there is a typo in it:

[if ($i1 == 8) || ($i1 == 24) ($i1 == 140) || ($i1 == 156) then 0 else 1]

try

[if ($i1 == 8) || ($i1 == 24) || ($i1 == 140) || ($i1 == 156) then 0 else 1]

now it should work.

but if you only need one outlet, you could also use [expr]. this would allow you
to do calculations after the comparison operators, too, which [if] wont allow.
("then (0+$f3) else 5-4")

Roman Thilenius's icon

[expr ($i1 != 8) || ($i1 != 24) || ($i1 != 140) || ($i1 != 156)]
when i am not wrong.

broc's icon

I think the expr would be

[expr ($i1 != 8) && ($i1 != 24) && ($i1 != 140) && ($i1 != 156)]

or

[expr ! (($i1 == 8) || ($i1 == 24) || ($i1 == 140) || ($i1 == 156))]

Eplusmusic's icon

Thanks for your responses guys. I think I need to get into the expr object more, as it looks super useful.

Is there a way in any of these objects to output different numbers depending on the number that goes in? I can't seem to figure this out using the if or the expr objects.

For example

8 -> 0
24 -> 1
56 -> 2
everything else -> 10

Thanks guys.

Luke Hall's icon
Max Patch
Copy patch and select New From Clipboard in Max.

If you want each new number you specify to return numbers that increase by 1 each time then the following example using [zl sub] will work.

Roman Thilenius's icon

one thing expr cannot do, is outputting nothing. you will have to use 0 or a placeholder and remove it later with [route 0]

Roman Thilenius's icon

24 -> 1
56 -> 2

is no problem:

expr ( ($i1==24) * 1 ) + ( ($i1==56) * 2 )

0 as output is problematic, as you need the 0 to let false results vanish from the addition.

Eplusmusic's icon

Thanks guys, I really need to learn more of this stuff, it is pretty awesome. :)

Peter McCulloch's icon

zl sub is definitely the way to go.

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

However, here's another way to tackle it using vexpr, which is a good skill to know for other problems like this.

Another tip: avoid using multiple objects or additional inlets to solve problems when you can solve it via a list. Spend some quality time with the zl objects and especially vexpr (like expr, but for lists: @scalarmode 1 is magic.) This will save you tons of time, make your code more dependable, and make your code more flexible. In my experience, code works best when you keep patchcords to a minimum.

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

Learn to look for general problems where you can and solve them well, then store them for use as abstractions. Yes, there is a time for specific solutions, but specific solutions are rarely reusable, and generally don't provide ways of expanding on an idea.