< Back to IRCAM Forum

Find and replace repeats

Hello,
I am trying to figure out how to make a “find and replace” function of some sort. What I want is for OM to look through a list of numbers, and if there are three occurrences of the same number (given the lists I am working with, it won’t be possible to have more than three), to replace one or two of those occurrences (whether it replaces one or two, and which of the occurrences it replaces, should be random) with a specific other number, maintaining the order of the list.
I want the replacements to map onto each other in the following way:
(2 3 4 5 6) <—> (7 8 9 10 11)
…so that, if there are three occurrences of 2, one or two of them is replaced by 7 and vice versa, 3 becomes 8 and vice versa, etc.
A possible example list could be:
(10 8 7 6 4 10 3 7 11 4 10 3 7 11 4)
Then one or two of the 10s should be replaced by 5s, of the 7s by 2s, and of the 4s by 9s. The numbers 8, 6, 3, and 11 should remain unaffected.
Another way of framing the problem is that each number 2-11 should be used at least once in the list, and that in order to make this happen each number has a “pair” which it can be replaced by.
Thanks in advance for any help.
Theo

Dear Theo,

Maube something like this?:

The patch:
findreplace.omp (13.1 KB)

Best
K

Hm, not quite, though thank you for putting in all this work. You definitely seem to be on the right track, and I would troubleshoot it myself but I don’t really understand the details of how your patch works.

If you look at the input list in your screenshot, the only values that occur three times are 10, 7, and 4. All other values should remain unchanged in the output, in the same position as in the input: the 3s, for example, should not be replaced by 8s in the output list, because there are only two instances of 3 in the input list.

There are also some problems with the substitutions themselves: 10 should not be replaced by 15, but by 5 (and conversely 5 should be replaced by 10, in the event that the input list contains three instances of 5). 7 should not be replaced by 12, but by 2; 11 not by 16, but by 6 (though in this example it should not be replaced at all, because 11 appears only twice in the input list). But the 4s seem to be behaving correctly: there are three of them in the input list, and in the output list two of those three are replaced by 9. It is also correct that 3s, generally, should be replaced by 8s, but in the input list there are only two occurrences of 3, so with the given input list they shouldn’t be replaced at all.

In other words, each number from 2 to 11 should appear once in the list, and there should be no numbers greater than 11. If there are less than 3 occurrences of a single number in the input list, then the corresponding number in the same “number pair” (I.e. 2 for 7, 3 for 8, etc.) will already be in the input list (because of the nature of the lists I am working with). Thus there is no need to make any replacements. If there are 3 occurrences of a single number in the input list, its corresponding number is not included in the input list; the replacement function I’ve described is intended to fix that.

I hope this is clearer. Thank you once again for putting in this work, and let me know if there’s anything more I can do to help figure it out.

Sorry just a typo…
Here is the corrected patch:
findreplace 2.omp (31.0 KB)