How to replace, in a list, all the 1 by 15 and all the 0 by 5

Aug 26, 2012 at 6:11pm

How to replace, in a list, all the 1 by 15 and all the 0 by 5

Hello,

How to replace, in a list, all the 1 by 15 and all the 0 by 5 ?
exemple : get 5 5 15 15 5 5 from 0 0 1 1 0 0

Best regards

All

#64106
Aug 26, 2012 at 6:49pm

Here’s a fairly concise solution using vexpr. But in general, for finding/replacing, regexp is very powerful, and if you’re comfortable with JavaScript you can accomplish such tasks quite well with js.

– Pasted Max Patch, click to expand. –
#231219
Aug 26, 2012 at 6:58pm

In my opinion a regular expression (regexp object) is the easiest way to do this:

– Pasted Max Patch, click to expand. –
#231220
Aug 26, 2012 at 7:31pm

I read the OP’s desired task backward. If the incoming list is known to contain only 0s and 1s, then regexp (à la ShelLuser’s solution) is the way to go. Or “vexpr $i1*15-10″.

#231221
Aug 27, 2012 at 5:39am

Hi,

If you deal with integers AFAIK numerical solution is more efficient,
and in your case the equation is easy to find :

– Pasted Max Patch, click to expand. –
#231222
Aug 27, 2012 at 6:09am

why not just [scale 0 1 5 15] ??

#231223
Aug 27, 2012 at 9:48am

Hi,

currently, [scale] wins ;-)

– Pasted Max Patch, click to expand. –
#231224
Aug 27, 2012 at 10:39am

I don’t think scale is such a good idea because it would change everything in the list. Including values which don’t need to be changed.

Although the OP doesn’t mention the presence of anything else in the list I do think its an option to keep in mind.

#231225
Aug 27, 2012 at 10:47am

[scale] still wins:

– Pasted Max Patch, click to expand. –

Also I’m somehow only just now discovering the [routepass] object. It is my new favorite thing.

#231226
Aug 27, 2012 at 10:50am

Hi,

you are right ; anyway without a large view, we can just speculate… i’m pretty sure that for this kind of stuff i would use [sel 0 1] and [t 5] [t 15] as usually i prefer small common object that a “all in one”.

– Pasted Max Patch, click to expand. –
#231227
Aug 27, 2012 at 10:53am

Ooh, I also didn’t think about using zl.group like that. Learnin’ new stuff erryday.

#231228
Aug 27, 2012 at 1:39pm

Than you for your help.
I can progress in my project…

All

#231229

You must be logged in to reply to this topic.