I initially hit the same issue and then changed my approach to avoid using stringreplace. The logic I ended up with was: find the first and last match of any of the digits or number words, combine those two into a single string and now replace words with digits on those knowing that there's no chance of overlaps.
SPOILERS BELOW: s =: 0 : 0 two1nine eightwothree abcone2threexyz xtwone3four 4nineeightseven2 zoneight234 7pqrstsixteen ) NB. initial "replace" verb that had the same issue r=:rplc&(','cut'zero,0,one,1,two,2,three,3,four,4,five,5,six,6,seven,7,eight,8,nine,9') nums=:','cut'one,two,three,four,five,six,seven,eight,nine,1,2,3,4,5,6,7,8,9' NB. working "replace" verb r=:{{ f=.(nums{~]i.<./@-.&0) ,(<./@-.&0)"1 I.E.&(' ',y) &> nums l=.(nums{~]i.>./@-.&0) ,>./"1 I.E.&(' ',y) &> nums ". -.&' ' (>f,l) rplc (','cut'zero,0,one,1,two,2,three,3,four,4,five,5,six,6,seven,7,eight,8,nine,9') }} +/ r &> LF cut s 281 On Sat, Dec 2, 2023 at 12:29 PM David Pinchbeck <dpinc...@gmail.com> wrote: > I'm trying to improve my J using Advent of Code, but am already a bit > stuck on the second part of day 1. It seems late enough in the day now to > ask this question without spoiling anything. > > On the example code one needs to turn the string 'eightwothree' into '8wo3' > I tried the following, which fails because the priority of stringreplace > comes from the left argument ordering: > > digs=:'0123456789' > > s=:'zero';'one';'two';'three';'four';'five';'six';'seven';'eight';'nine' > > substr=:(s,. <"0 digs) > > g=:{{ substr stringreplace y}} > > g 'eightwothree' > > eigh23 > > How can I get the earliest appearance on the right side to be replaced > first instead? > > Thanks, > David > -- > David Pinchbeck > Associate Professor of Mathematics > St. Joseph's College > Standish, ME > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm