Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Splitting string based on substring separator

From: gerardcote:sympatico:ca at: 21-Dec-2002 21:54

Hi Frantisek, As I thought of a way to get more exercises learning REBOL for myself, before submitting more advanced answers to the FOSSE FAQ I have submitted to, I found the one below a simple enough exercice for me to try, even if it is not recursive as it could be and not as elegant as it could also be but one step at a time is my new way to go... So below is my first ROUGH but functioning solution. No function is used for the moment since I had enough to test of REBOL but a lot of (useful and not so useful) comments - in French and English but you can strip them all if you want to keep the essential. My next try will have some recursive function since this is already in a near form for doing so. And then I'll want to look for other more elegant ways (that is available in a native form) to do so. I also know that I could have used the Higher Func stuff form Ladislav and others but this is not my goal for the moment. I hope that this can be of some help to other newbies like me. And this is really my thought adapted from my previous Visual Basic experience that this reflects. So any suggestion to improve this work in its current implementation form is welcome for discussion or any other reason ... Regards, Gerard ----- Original Message ----- From: "Frantisek Fuka" <[fuka--fuxoft--cz]> To: <[rebol-list--rebol--com]> Sent: Saturday, December 21, 2002 6:48 PM Subject: [REBOL] Splitting string based on substring separator
> Let's say I've got this string > > "HELLOsepP E O P L EsepHOWsepAREsepYOU" > > and I want to split it to substrings based on the separator string (in > this case, "sep"). So I want this result: > > ["HELLO" "P E O P L E" "HOW" "ARE" "YOU"] > > I know I can do this using parse and build the resulting block > programatically, but isn't there a simpler and cleaner solution? For > example, if the separator was just one character long, I know I'd just > write: > > result: parse/all string-to-parse separator-character > > Thanks >
My first try that works like you report it should do : ; Help for the translation ; For English ppl. replace/all 'ch with 'original-string ; 'chf with 'final-string ; 'mot with 'next-word ; 'sep with 'separator ; 'Original-string to search for Words to parse is called - 'ch for Chaine (French) ; ================================================================= ch: "HELLOsepP E O P L EsepHOWsepAREsepYOU" ; probe chf should return ["HELLO" "P E O P L E" "HOW" "ARE" "YOU"] ; The Returned 'final-string is called - 'chf for Chaine Finale (French) ; ========================================================================== chf: copy [] ; Separator (Here it is 'word fixed for helping during the test phase) ; =====================================================----=========== sep: "sep" while [ not empty? ch ][ either find ch sep [ mot: copy/part find ch sep ch either empty? mot ; Fr: Si 'mot vide - alors 'sep au début de chaîne ou chaîne vide - tenter de sauter 'sep ; pour trouver le prochain 'mot s'il existe ; Eng: If 'found-word not empty then 'sep is found at beginning of original-string or ; original-string is empty ; then try to skip 'separator for finding the 'next-word if it exists [ ch: skip ch length? sep ] ; Fr: Si 'mot existe alors le cueillir et se placer après le prochain 'mot et ; le prochain 'sep ; Eng: If 'next-word exists then append it and skip the 'next-word and next 'Separator [ append chf mot ch: skip ch length? mot ch: skip ch length? sep ] ] ; Fr: si seul un dernier mot existe après le dernier sep trouvé alors le cueillir et terminer ; Eng: If only a last 'next-word exists after the last found 'separator then append it and end [ mot: ch append chf mot ch: skip ch length? mot ] ] ; Other 'Original-string tests that I tried for- ; ============================================== ch-0: "" ; probe chf should return [] ch-1: "sepWORDsep" ; probe chf should return ["WORD"] ch-2: "sepP E O P L EsepHOWsepMANYsepAREsepYOU" ; probe chf should return ["P E O P L E" "HOW" "MANY" "ARE" "YOU"] ch-3: "sepP E O P L EsepsepHOWsepMANYsepAREsepYOUsep" ; probe chf should return ["P E O P L E" "HOW" "MANY" "ARE" "YOU"] ch-4: "HELLO" ; probe chf should return ["HELLO"] ch-5: "sep" ; probe chf should return []