Using Dyalog APL I would like to take a Mask Vector where '-' means use the original character and any other character is to replace that position in the Data Vector. So in the example below, Mask 'a' is used on Vector 'b' to create Vector 'c'.
┌──────┬────────┬────┐
│X--Y-Z│--34--91│-+-+│ Mask Vector a
└──────┴────────┴────┘
┌──────┬────────┬────┐
│abcdef│hijklmno│stuv│ Data Vector b
└──────┴────────┴────┘
┌──────┬────────┬────┐
│XbcYeZ│hi34lm91│s+u+│ Resultant Vector c
└──────┴────────┴────┘
The vectors contain strings of different lengths. I do not wish to use any Regular Expression or Search and Replace function built into APL.
I created code to perform this function, but it seems far too complicated as it is creating matrices, rotating characters around, and pulling data out of rows. I am sure there is an easier way to perform this function in APL. I am interested in seeing what more experienced APL developers come up with.
The principal operation is
(where/¨data) ← fill, which is an example of "selective assignment".This operation can be more succinctly defined as an operator:
merge ← {d←⍵ ⋄ (w/¨d)←(w←⍺⍺≠⍺)/¨⍺ ⋄ d}And called in the form
mask ('-' merge) data