1. String Search and Replace

I have been going over the Euphoria doc. and am impressed with the speed,
and data structure simplicity.
Can someone post a string search and replace function? One of the reasons I
am looking around for a better language to program in is because the ones I
use, APL and J (both interpreted) are too slow for some of things I want to
do - especially string search and replace. I think that Euphoria will
benchmark quite a bit better than what I am currently using in J.

Thanks - Jay Daulton

new topic     » topic index » view message » categorize

2. Re: String Search and Replace

On Fri, 12 Nov 1999, Jay Daulton wrote:

> I have been going over the Euphoria doc. and am impressed with the speed,
> and data structure simplicity.
> Can someone post a string search and replace function? One of the reasons I
> am looking around for a better language to program in is because the ones I
> use, APL and J (both interpreted) are too slow for some of things I want to
> do - especially string search and replace. I think that Euphoria will
> benchmark quite a bit better than what I am currently using in J.
>
> Thanks - Jay Daulton

Here is a simple example I just whipped up:

-- tested code starts here --
sequence string1, string2

function replace( sequence string, sequence findstring, sequence
replacestring )
  integer loc, len

  len   = length( findstring )
  loc = match( findstring, string )

  if loc then
    return string[1..(loc - 1) ] &
           replacestring &
           string[ (loc + len)..length( string ) ]
  else
    return string
  end if

end function


string1 = "Euphoria is fast, flexible and fun; simple, safe, and sexy!\n"
string2 = replace( string1, "sexy", "super sexy" )
puts( 1, string1 )
puts( 1, string2 )
-- end tested code --

Sorry about lack of comments but it should be pretty straight-forward.  If
the sequence is not found, the function simply returns the original,
unmodified string.  You can modify it to return whatever you want...

Hope this helps,
Brian

new topic     » goto parent     » topic index » view message » categorize

3. Re: String Search and Replace

Thanks - I will give it a try.

Jay Daulton

-----Original Message-----
From: Brian K. Broker [mailto:bkb at CNW.COM]
Sent: Friday, November 12, 1999 3:12 PM
To: EUPHORIA at LISTSERV.MUOHIO.EDU
Subject: Re: String Search and Replace


Here is a simple example I just whipped up:

-- tested code starts here --
sequence string1, string2

function replace( sequence string, sequence findstring, sequence
replacestring )
  integer loc, len

  len   = length( findstring )
  loc = match( findstring, string )

  if loc then
    return string[1..(loc - 1) ] &
           replacestring &
           string[ (loc + len)..length( string ) ]
  else
    return string
  end if

end function


string1 = "Euphoria is fast, flexible and fun; simple, safe, and sexy!\n"
string2 = replace( string1, "sexy", "super sexy" )
puts( 1, string1 )
puts( 1, string2 )
-- end tested code --

Sorry about lack of comments but it should be pretty straight-forward.  If
the sequence is not found, the function simply returns the original,
unmodified string.  You can modify it to return whatever you want...

Hope this helps,
Brian

new topic     » goto parent     » topic index » view message » categorize

4. Re: String Search and Replace

From: Jay Daulton <JayD at ADVANCEDBIONICS.COM>

Subject: String Search and Replace

> Can someone post a string search and replace function?
> I think that Euphoria will benchmark quite a bit better than
> what I am currently using in J.

Take a look at the match() function.
Also, you need to decide whether to replace only the first instance,
and return, or whether you want to replace all instances without
asking. Below is one possibility:

sequence t
t = "Now is the time for all good men to come to the aid of the party"

function Replace(sequence target, sequence old, sequence new)
integer i,j
 i = match(old,target)
 if i > 0 then
    j = i + length(old)
    return target[1..i-1] & new & target[j..length(target)]
 else
  return target
 end if
end function

puts(1,Replace(t,"men","women"))

Regards,
Irv

new topic     » goto parent     » topic index » view message » categorize

5. Re: String Search and Replace

I need to replace all matches without asking. Is there a library (maybe at
someone's website) of commonly used functions like search and replace,
different random number generators, probability distributions, misc. string
functions? In the C++ world they have what they like to consider "generic"
programming which makes use of standard template libraries - like SGI's.
There are literally hundreds of utility functions for use on what they call
"containers". There are about 30 of those ranging from bags to sets to lists
to arrays..... In Euphoria's case fortunately there is only one container
called a sequence. Most of the C++ compilers ship with a template library.

Thanks - Jay Daulton

-----Original Message-----
From: Irv Mullins [mailto:irv at ELLIJAY.COM]
Sent: Friday, November 12, 1999 1:59 PM
To: EUPHORIA at LISTSERV.MUOHIO.EDU
Subject: Re: String Search and Replace


From: Jay Daulton <JayD at ADVANCEDBIONICS.COM>

Subject: String Search and Replace

> Can someone post a string search and replace function?
> I think that Euphoria will benchmark quite a bit better than
> what I am currently using in J.

Take a look at the match() function.
Also, you need to decide whether to replace only the first instance,
and return, or whether you want to replace all instances without
asking. Below is one possibility:

sequence t
t = "Now is the time for all good men to come to the aid of the party"

function Replace(sequence target, sequence old, sequence new)
integer i,j
 i = match(old,target)
 if i > 0 then
    j = i + length(old)
    return target[1..i-1] & new & target[j..length(target)]
 else
  return target
 end if
end function

puts(1,Replace(t,"men","women"))

Regards,
Irv

new topic     » goto parent     » topic index » view message » categorize

6. Re: String Search and Replace

On Fri, 12 Nov 1999 17:06:50 -0800, Jay Daulton <JayD at ADVANCEDBIONICS.COM>
wrote:

>I need to replace all matches without asking.

That would be a minor change to my previous solution... Just use a
recursive call on the remainder of the string:

-- start code --
sequence string1, string2

function replace( sequence target, sequence old, sequence new )
  integer len, loc

  len = length( old )
  loc = match( old, target )

  while loc do
    return target[1..(loc - 1) ] &
           new &
           -- recursive call on remainder of string --
           replace( target[(loc + len)..length( target )], old, new )
  end while

  return target

end function


string1 = "Euphoria is fast, flexible and fun; simple, safe, and sexy!\n"
string2 = replace( string1, "and", "or" )
puts( 1, string1 )
puts( 1, string2 )
-- end code --

>Is there a library (maybe at
>someone's website) of commonly used functions like search and replace,
>different random number generators, probability distributions, misc. string
>functions?

The best place to look is http:\\www.rapideuphoria.com\
If you can't find it in recent user contributions or the archives, then you
can try different Eu pages listed on "Other Euphoria Web sites".  All other
libraries come with the public domain version.

-- Brian

new topic     » goto parent     » topic index » view message » categorize

7. Re: String Search and Replace

On Tue, 16 Nov 1999 11:53:06 -0500, Brian Broker <bkb at CNW.COM> wrote:

Just a side note, I noticed I changed my solution a bit (besides changing
variable names):

> ...
>  while loc do
>    return target[1..(loc - 1) ] &
>           new &
>           -- recursive call on remainder of string --
>           replace( target[(loc + len)..length( target )], old, new )
>  end while
>
>  return target
> ...

It doesn't change the behavior but the above might be easier to follow when
written like:

  if loc then
    return target[1..(loc - 1) ] &
           new &
           -- recursive call on remainder of string --
           replace( target[(loc + len)..length( target )], old, new )
  else
    return target
  end if

new topic     » goto parent     » topic index » view message » categorize

8. String Search and Replace

--942851895 at router-8.camnet.com
Content-Description: "cc:Mail Note Part"

G'day again.

Below are the results of comparing Brian's replace all with mine and
mine's faster (for all it matters). Attached is the testing program.

ZB

Let's see what other dumb mistakes I can make today...

Contents:
Windows NT4. VirtualPC. G3/300
==============================

Broker_replace did 1 to 1 replace in 1.3 seconds
Axtens_replace did 1 to 1 replace in 0.82 seconds

Broker_replace did 2 to 3 replace in 0.61 seconds
Axtens_replace did 2 to 3 replace in 0.54 seconds

Broker_replace did 4 to 0 replace in 0.41 seconds
Axtens_replace did 4 to 0 replace in 0.35 seconds

Broker_replace did 4 to 8 replace in 0.46 seconds
Axtens_replace did 4 to 8 replace in 0.44 seconds

Windows NT4. Toshiba 110CS.
===========================

Broker_replace did 1 to 1 replace in 1.64 seconds
Axtens_replace did 1 to 1 replace in 1.11 seconds

Broker_replace did 2 to 3 replace in 0.81 seconds
Axtens_replace did 2 to 3 replace in 0.76 seconds

Broker_replace did 4 to 0 replace in 0.54 seconds
Axtens_replace did 4 to 0 replace in 0.43 seconds

Broker_replace did 4 to 8 replace in 0.65 seconds
Axtens_replace did 4 to 8 replace in 0.64 seconds


Contents:


--942851895 at router-8.camnet.com

new topic     » goto parent     » topic index » view message » categorize

9. Re: String Search and Replace

What was the content and size of the text string? What was the target string
and the replace string?

Thanks - Jay Daulton

-----Original Message-----
From: Bruce M. Axtens [mailto:zaphod_beeblebrox at SIL.ORG]
Sent: Wednesday, November 17, 1999 1:36 PM
To: EUPHORIA at LISTSERV.MUOHIO.EDU
Subject: String Search and Replace


G'day again.

Below are the results of comparing Brian's replace all with mine and
mine's faster (for all it matters). Attached is the testing program.

......

new topic     » goto parent     » topic index » view message » categorize

10. Re: String Search and Replace

--942922863 at router-8.camnet.com
Content-Description: "cc:Mail Note Part"

Thus spake ce Jay Daulton on Wed, 17 Nov 1999:
>Subject: Re: String Search and Replace
>
>What was the content and size of the text string? What was the target string
>and the replace string?
>
>Thanks - Jay Daulton

Whoops. And there I was thinking that my Euphoria frontend to ccMail was working
properly. Oh well ... Attached is the source and the results from two machines.

ZB

P.S. The test did replaces on a short string. The results might be different on
large chunks of text where Euphoria is having to stress its memory manager.


--942922863 at router-8.camnet.com

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu