Sorry that "explicit" in the first paragraph should read "tacit"
On Mon, Jun 18, 2012 at 11:35 PM, Ric Sherlock <tikk...@gmail.com> wrote: > On Mon, Jun 18, 2012 at 9:15 PM, Gian Medri <gianme...@gmail.com> wrote: >> Hi! >> >> I shall replace some text in a vector. My function is >> Replace=:13 :'(p{.y),(>1{x),(($>0{x)+p=.I. (>0{x) E. y)}.y' >> Example : ('can be';'is') Replace 'Life can be good' >> Life is good >> I would like to see if there is a better solution, more J-like. >> > > I'm not sure what the desired behaviour for Replace is for strings > where the text to be replaced doesn't occur or occurs more than once > but if you're wanting an explicit version of that code then here is > one: > > Replace2=: (] {.~ 0&{::@[ I.@E. ]) , 1&{::@[ , ] }.~ #@(0&{::)@[ + > 0&{::@[ I.@E. ] > ('can be';'is') Replace2 'Life can be good' > Life is good > > However, for something like that I'd tend to move to an explicit > definition. Something like the following will handle most edge cases > nicely as well as being easier to maintain. > > replace3=: 4 : 0 > 'findtxt repltxt'=. boxopen x > out=. findtxt (] <;.1~ (1:)`(I.@E.)`(0 #~ #@])}) findtxt,y > out=. (#repltxt)}. ; (repltxt , (#findtxt)&}.) &.> out > ) > > The second line boxes the initial string wherever the findtxt begins. > The 3rd line then drops the findtxt from each box and catenates the > repltxt > > If you are wanting to do this routinely though then I'd suggest using > the base library verbs > stringreplace or rplc which also handle multiple find/replace pairs. ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm