|
Pages: [1]
|
 |
|
Author
|
Topic: Multiple search & replace in Rebol ? (Read 194 times)
|
|
rebolnotes
|
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
|
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
|
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
|
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 
|
|
|
|
|
Logged
|
|
|
|
|
btiffin
|
Hi again, Once again ... this is quick and poorly tested, but start here instead  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.  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.  Cheers, Brian
|
|
|
|
|
Logged
|
|
|
|
|
rebolnotes
|
Wow thanks a lot btiffin it's so kind of you  Will come back in a week or so when I will have studied the script.
|
|
|
|
|
Logged
|
|
|
|
|
btiffin
|
Stick with REBOL and you'll find most rebols are friendly, helpful folk.
Cheers, Brian
|
|
|
|
|
Logged
|
|
|
|
|
rebolnotes
|
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 
|
|
|
|
|
Logged
|
|
|
|
|
|
Pages: [1]
|
|
|
 |
News: 01-09-08 Alpha version of REBOL 3 has been released!
2169 Posts in 562 Topics by 1224 Members
Latest Member: thyptoste
|