> The code for q longer than p should work for the case
> where q is not longer than p.  The extra code is probably
> due to its being faster.

rep1=: 4 : 0
 'p q'=. y
 b=. p E. x
 ((b i. 1){.x) , ; q&,&.>(#p)}.&.>b <;.1 x
)

   x=: 1e6 $ 'by the people, of the people, for the people, '
   y=: 'people';'rich'
   x (replace -: rep1) y
1
   ts=: 6!:2 , 7!:[EMAIL PROTECTED] NB. time and space
   ts 'x replace y'
0.0380342 6.55693e6
   ts 'x rep1 y'
0.132868 2.1696e7



----- Original Message -----
From: Roger Hui <[EMAIL PROTECTED]>
Date: Thursday, November 13, 2008 8:04
Subject: Re: [Jprogramming] tacit stringreplace
To: Programming forum <[email protected]>

> I use the following on occasion.  It assumes that the ff
> character does not occur in the text or in the replacement
> string.  Also, if overlapping substrings need to be handled,
> you have to replace E. by nos in
> http://www.jsoftware.com/jwiki/Essays/Non-Overlapping_Substrings
> 
> The code for q longer than p should work for the case
> where q is not longer than p.  The extra code is probably
> due to its being faster.
> 
> ff=: {: a.
> 
> NB. x is text; y is (p;q); replace all occurrences of p by q in x
> replace=: 4 : 0
>  'p q'=. y
>  d=. p -&# q
>  b=. p E. x
>  if. 0<:d do.     NB. replacement string not longer
>   ff -.~ (q,d$ff) ((I. b) +/ i.#p)}x
>  else.            NB. replacement string longer
>   ((b i. 1){.x) , ; q&,&.>(#p)}.&.>b <;.1 x
>  end.
> )
> 
> 
> 
> ----- Original Message -----
> From: "L.Tomei" <[EMAIL PROTECTED]>
> Date: Thursday, November 13, 2008 7:50
> Subject: RE: [Jprogramming] tacit stringreplace
> To: [email protected]
> 
> > 
> > So, let we start to the target of 100.000 posts on J forums.
> > 
> > Coming back to stringreplace, I understand your explanation 
> > about "tacit vs
> > explicit".
> > But I still think that the stringreplace verb defined in 
> > string.ijs could be
> > improved, at least for some kinds of replacements.
> > My feel is that 50 (about) lines of J code, even if optimized, 
> > are too much
> > for fhis function.


----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to