substituting / replacing multiple numbers in a list

Michael's icon

I have 2 dynamic lists (of variable length) as below

Eg 1:
List 1: 1 0 2 0 3 0 4 0
List 2: 2 7 3 8

I would like to process these lists to get 1 0 2 7 3 8 4 0

Eg 2:
List 1: 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0
List 2: 1 1 4 5 7 1
Result: 1 1 2 0 3 0 4 5 5 0 6 0 7 1 8 0

Any help is much appreciated.

Source Audio's icon

Something in that lists needs to be non variable in order to merge them using any rules.
Like in example 1 you would replace list 1 items starting from index 2 (counted from zero)
with entire list 2.
or is it only first 4 items of list 2 ?
Something else ?
What would be rule for example 2 ?
it looks as :
replace list 1 items 0 1, 6 7, 12 13 etc
with list 2 items 0 1, 2 3, 4 5, 6 7 etc

here is one example using coll for your example 2
which seems to have clear substitute rules

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

Michael's icon

In example 1, the integers contained in list 2 represent the following:

2 and 3 = nth location of list 1 where nth = (x*2) -1.

7 and 8 = numbers to be substituted into list 1

the same applies to example 2.

Hope this makes sense.


Roman Thilenius's icon


1. find and delete double entries (zl)

2. merge (zl)

... that sorting part... is more difficult.
(no, wait...)

you might want to look into the "replace and "insert" messages of [coll] for that kind of stuff.

coll is slow, but the visual feedback of what happens is priceless.

Roman Thilenius's icon


thee minutes later i still dont have the full picture, but... it reminds me a bit of reading bitwise from files, and that is how i would start here: write both lists into a [coll] or a [zl nth], and then you can do every procedural task (if... then...goto) you can image with the list elements.

Source Audio's icon

You could have explained that in easier way from the beginning.
list 2 contains pairs of index / value to substitute in list 1.
Then all is so easy.
You just modify my coll example

expr $i1*2 is left in there in case you need to use zero based index for some other
list processing, which would then be expr $i1*2 -1
for coll, simple * 2 would have done.
coll counts stored items starting with 1,
that's why nth = (x*2) -1 has to be replaced with nth = (x*2)
-----------
Maybe all this could be done using zl objects alone.
But coll is much better in case you also need to store lists.