Change selected integers into others


#1

hi there, i coded an object that outputs choosen values if the input is equal to certain values. (kind of like a special case of select in puredata) can somebody have a look why if there is no hit (no numbers match) there is still the first value output? i would expect the value to stay the same as the last "hit" but it always bounces to the first when there is no match...

find attached the object.

==output.axo (1.6 KB)


#2

It's better to store the incoming values in a variable, and then output the value of the variable to the outlet. Fixed your code.

==output.axo (1.7 KB)


#3

this is a false expectation :wink:

@lokki for all outlets you must in every k-rate set its value,
in your code, outlet_out is 'undefined', as previous values are not copied across...
so, if the inlet does not match attr1,2,3 or 4....
you should not expect the outlet to retain the value from the previous k-rate cycle,
as @janvantomme replied, if you want this behaviour then you should explicitly store the value in a variable, and then use this as the basis for the outlet_out

(btw, it would also be more efficient in this case to use and if/else ladder, and set hit when you get a match, to avoid the extra comparisons... also given these are integers you should be able to use a switch statement instead, putting the 'non-hit' logic in the default clause)

note: i didnt lookout at jan's example, which I'm sure does the above :slight_smile:


#4

I just added the variables to show how it should be done, didn't optimize any further with if/else or a switch statement.


#5

cool thanks. noob here.


#6

@thetechnobear, you mean like this,right?

switch (inlet_in) {

case attr_in0:
valueHit = true;
outValue = attr_out0;
break;
case attr_in1:
valueHit = true;
outValue = attr_out1;
break;
case attr_in2:
valueHit = true;
outValue = attr_out2;
break;
case attr_in3:
valueHit = true;
outValue = attr_out3;
break;
default:
valueHit = false;
}

outlet_hit = valueHit;
outlet_out = outValue;

this works perfectly, just wanted to make sure it is the "right" way. (i also tried the if else ladder, also works perfectly)


#7

That should indeed work. You may want to add a break; for the default value as well. It may not be necessary if you put the default as the last thing to check, but it's good practice.


#8

yes, its the way I do it... rather than 'right', the if/else ladder is as efficient, but not as clear... (to me at least)

btw, your valueHit would not be required, as every path sets its value, so you can use outlet_hit, no need for an extra variable which will take up RAM.
(outValue is required, as one path (default) does not set the outlet_out, which was the source of your issue)


#9

perfect and corrected on my side, thanks!