[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Aug 16 12:16:21 2003 
Subject:Re: listing directories and chario 
From:ug57dsm 
Volume-ID:1030816.01 

Stephen Leach wrote:
> Hi David,
> 
> 
>>OK, the pop11 part of the server is just about finished... have a look
>>(attached gzip file). I just need to know how to replace a substring...
>>for example
>>
>>replace('hello, world', 'he', 'lox')=>
>>** loxllo, world
>>
>>but that shouldn't be hard to do (is there a procedure that does it already?
> 
> 
> I don't know of one ... odd ... must be one somewhere .... OK here we
> go ....
> 
> Assuming you want just the first match replaced, I would write it this
> way
> [code + snip]

I thought there would be one already... couldn't find any on ref
strings, thats why I asked... maybe one should be added to the main
distribution?

anyway, I had this same problem in java, where I made a 1 line method to
handle the problem... the pop11 version couldn't be one line long
because there is no proper ?: operator (I know if...then...else...endif
and the stack can do the same, but its longer), but its probably better
that way... this is how I defined it:

/* replace: Replace instances of text in a string. i.e.,
  *          replace('helloo', 'oo', 'p') = 'help'
  * str:     The original string
  * orig:    The text to replace
  * repl:    The stuff to replace it with
  * retval:  The resulting string after the replacement
  */
define replace(str, orig, repl) -> retval;
     lvars n, strend, strstart;
     str -> retval;

     if str = orig then
         repl -> retval;
         return;
     endif;

     if (issubstring(orig, str) ->> n)
     and str /= ''
     and orig /= '' then
         substring(1, n - 1, str) -> strstart;
         substring(n + length(orig), length(str) - length(orig) - n + 1,
str) -> strend;
         strstart <> repl <> replace(strend, orig, repl) -> retval;
     endif;
enddefine;


-- 
=================================================
  The future of HTML mail is clearly > /dev/null.
=================================================
  Two of the most famous products of Berkeley are
LSD and Unix. I don t think that is a coincidence
=================================================