Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Multiple search & replace in Rebol ?
Pages: [1] Print
Author Topic: Multiple search & replace in Rebol ?  (Read 328 times)
rebolnotes
Newbie
*
Offline Offline

Posts: 23


View Profile
Multiple search & replace in Rebol ?
« on: March 15, 2008, 01:23:31 PM »

Hi,

Want to replace in a text a list of n words by another list of n words. Of course I could use replace/all on the whole text n times but isn't there any optimal way to do with parse function or any other functions without reading the whole text several times but only once ?

Thanks.
Logged
btiffin
Jr. Member
**
Offline Offline

Posts: 58


View Profile
Re: Multiple search & replace in Rebol ?
« Reply #1 on: March 17, 2008, 01:44:31 AM »

Hi,

   It may not be high performance but I don't think you'll beat the simplicity of replace/all for this.


data: read file
words: [o1 r1 o2 r2 o3 r3]
foreach [org rep] words [
    replace/all data org rep
]

Note; this in itself is ripe for data dependent bugs if there is any overlap in originals and replacements and the order they occur etc...

But ... you could try something like  (and be aware, this is not tested)


;; replacements is  o r  pairs  one replaced with four  two replaced with five
replacements: ["one" "four" "two" "five" "three" "six"]
alternates: copy []
foreach [o r] replacements [append alternates o  append alternates '|]
remove back tail alternates
;; alternates should now look like   ["one" | "two" | "three"]
parse data [
   some [()
       mark:
       copy original alternates (
           print ['changing original 'at index? mark]
           remove/part mark length? original
           mark: insert mark select replacements original
       )
       :mark
       | skip
    ]
]

Cheers,
Brian
Logged
btiffin
Jr. Member
**
Offline Offline

Posts: 58


View Profile
Re: Multiple search & replace in Rebol ?
« Reply #2 on: March 17, 2008, 02:32:26 AM »

Ok, went and tested it.  That version will replace  someone  with somefour.

So it could be

data: {one: some data with a one and a two and a three in it and another one
and now a line with one two three four five and six in it
and now a word with someone embedded}
replacements: [" one " " four " " two " " five " " three " " six "]
alternates: copy []
foreach [o r] replacements [append alternates o  append alternates '|]
remove back tail alternates

parse/all data [
   some [()
       mark:
       copy original alternates (
           print ['changing original 'at index? mark]
           remove/part mark length? original
           mark: insert mark select replacements original
       )
       :mark
       | skip
    ]
]


But that will miss the initial  one: at the start of the test data string, the one at the end of the first line etc...
So, getting this correct for all cases will be an exercise in word boundaries as well.

Cheers,
Brian

Oh, in short .. my sample solutions don't work - more required.
Logged
rebolnotes
Newbie
*
Offline Offline

Posts: 23


View Profile
Re: Multiple search & replace in Rebol ?
« Reply #3 on: March 17, 2008, 07:24:25 AM »

Hi thanks a lot for the starting solution. I'm going to work it though I'm not sure to succeed as I have hard time with Rebol Parse in the past but I still prefer parse to regular expression though that's why I like Rebol Smiley
Logged
btiffin
Jr. Member
**
Offline Offline

Posts: 58


View Profile
Re: Multiple search & replace in Rebol ?
« Reply #4 on: March 17, 2008, 09:21:19 AM »

Hi again,

Once again ... this is quick and poorly tested, but start here instead Smiley


rebol []

data: {one: some data with a one and a two and a three
in it and another one and now a line with one two three
four five and six in it.  And now a word with someone or sometwo
embedded, ending in two. and three}

replacements: ["one" "four" "two" "five" "three" "six"]
alternates: copy []
foreach [o r] replacements [append alternates o  append alternates '|]
remove back tail alternates

enders: charset [#" " #"^-" #"^/" #":" #"," #"."]
bounds: [enders | end]

parse/all data [
   mark:
   copy original alternates bounds (
       print ['changing original 'at index? mark]  ;; always 1
       remove/part mark length? original
       mark: insert mark select replacements original
   )
   :mark
   some [()
       enders
       mark:
       copy original alternates bounds (
           print ['changing mold original 'at index? mark]
           remove/part mark length? original
           mark: insert mark select replacements original
       )
       :mark
       | skip
   ]
]

You'll need to muck about with the  enders  charset at a minimum.
This should catch originals in column 1 and then only orgs not embedded in other words.

Note;  I always add () when I start a some, because if the alternates include
a none case or the order is wrong you can get infiniite loops.  some [()  gives
REBOL a chance to react to the escape key.  Smiley  It's a cheat, but it's a some what inexpensive safety blanket for all parse work.

And  I expect I haven't thought of all the cases and I hope another rebol sees this and corrects any mistakes.  Smiley

Cheers,
Brian
Logged
rebolnotes
Newbie
*
Offline Offline

Posts: 23


View Profile
Re: Multiple search & replace in Rebol ?
« Reply #5 on: March 18, 2008, 02:24:50 AM »

Wow thanks a lot btiffin it's so kind of you Smiley

Will come back in a week or so when I will have studied the script.
Logged
btiffin
Jr. Member
**
Offline Offline

Posts: 58


View Profile
Re: Multiple search & replace in Rebol ?
« Reply #6 on: March 18, 2008, 10:26:56 AM »

Stick with REBOL and you'll find most rebols are friendly, helpful folk.

Cheers,
Brian
Logged
rebolnotes
Newbie
*
Offline Offline

Posts: 23


View Profile
Re: Multiple search & replace in Rebol ?
« Reply #7 on: March 23, 2008, 08:53:17 AM »

I'm struggling with ftp at the moment so didn't have time to look at it yet but sure I will cause I really need that stuff Wink
Logged
Pages: [1] Print 
Rebol Talk Forum  |  Getting Started  |  Newbie Help  |  Topic: Multiple search & replace in Rebol ?
Jump to:  

  
Quick Search...

Advanced search
  
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 29, 2008, 02:16:20 PM
Username: Password: Session Length:
  

News: 01-09-08

Alpha version of REBOL 3 has been released!


  
2234 Posts in 582 Topics by 1745 Members
Latest Member: vectlycle

  Rebol Talk Forum | Powered by SMF 1.0.9.
© 2001-2005, Lewis Media. All Rights Reserved.

RT design by Defiant Pc