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

Finding the previous value in a string

 [1/6] from: tbrownell:ya:hoo at: 11-Aug-2000 12:33


Hello all... Is there some way to find a value in a string that immediately proceeds another eg... string: "This is a string with some words" How would i find the word immediately before the word with ... in this case the word "string" ? Kinda like select, but working backwards? TBrownell

 [2/6] from: ssayer:acuson at: 11-Aug-2000 16:47


; How about this: string: "This is a string with some words" select head reverse to-block string 'with ; Or... to-string select head reverse to-block string 'with ; ... if you need it converted back into a string. Later, <SS> On Fri, 11 Aug 2000 [tbrownell--yahoo--com] wrote:

 [3/6] from: galtbarber:mailandnews at: 11-Aug-2000 17:32


Here are several ways to do it: blk: [This is a block with some words] i: find blk 'with print first back i print first skip i -1 print first at i string: "This is a string with some words" blk: parse/all string " " print first back find blk "with" i: find string " with" j: find/reverse i " " print copy/part next j i -Galt

 [4/6] from: alex:pini:mclink:it at: 12-Aug-2000 0:00


>- Open Your Mind -<
Quoting from [ssayer--acuson--com's] message (11-Aug-00 22:47:59). s> ; How about this: s> s> string: "This is a string with some words" s> select head reverse to-block string 'with ... or maybe ... first back find to-block string 'with s> ; Or... s> s> to-string select head reverse to-block string 'with s> s> ; ... if you need it converted back into a string. ... or maybe ... to-string first back find to-block string 'with There is some caveat with my approach, but I'm falling down due to severe lack of sleep and I can't remember it, so goodnight. :-) Alessandro Pini ([alex--pini--mclink--it]) Have you been getting enough sleep? "More or less. Mostly less." (E.M.H.P. & Janeway)

 [5/6] from: ssayer:acuson at: 11-Aug-2000 19:00


Those are good too. I first thought "why don't they use find?" when I read the message too, but it occured to me that demonstrating a way to reverse select might be in order as they asked specifically for that type of approach. I'll have to think about this point more but why isn't there a reverse refinement on select anyway? Later, <SS> On Sat, 12 Aug 2000 [alex--pini--mclink--it] wrote:

 [6/6] from: larry::ecotope::com at: 11-Aug-2000 17:32


Hi Allessandro Yes, there are at least 2 caveats to the approach of converting the string to a block of REBOL words with
>> string: "This is a string with some words"
== "This is a string with some words"
>> b: to-block "This is a string with some words"
== [This is a string with some words]
>> type? first b
== word! The first caveat is that not everything we may want as "words" is a valid REBOL word.
>> to-block "This is a string |\funny"
** Syntax Error: Invalid word -- |\funny. ** Where: (line 1) This is a string |\funny So the conversion fails. The second caveat is that the REBOL dictionary only holds 2558 words.
>> repeat j 3000 [append b to-word join 'word j]
** Internal Error: No more global variable space. ** Where: to word! :value
>> length? first rebol/words
== 2558 CAUTION: There is no way to remove words from the dictionary, the GC does not touch them. In order to create a new word after this experiment, you will have to start a new REBOL session. So if there are many unique "words" in the string, you will permanently tie up space in the REBOL dictionary. Galt's solution: string: "This is a string with some words" blk: parse/all string " " print first back find blk "with" is much better, because it converts the string to a block of string! values rather than a block of REBOL words. If you want to just parse on any whitespace (including linefeed, etc), you can use
>> parse string none
== ["This" "is" "a" "string" "with" "some" "words"] which in this case gives the same result as parse/all string " " -Larry