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

[REBOL] Re: Parsing out strings

From: greggirwin:mindspring at: 29-Apr-2004 21:42

Hi Stuart, M> I am looking at "clear find" and "remove/part" and things like this M> but I am getting very confused as to what to use when, I am more of M> a left$, mid$, right$ kind of guy. I specialized in VB for a loooong time, so I know where you're coming from. Let's start with some simple functions: left: func [s len][copy/part s len] right: func [s len][copy skip tail s negate len] mid: func [s start len][copy/part at s start len] set-mid: func [ series [series!] new-data start [integer!] /part len [integer!] ][ len: any [len length? new-data] head change/part at series start new-data len ] I thought I would use these a lot (which is why I wrote them when I first found REBOL), but I never have. :\ These should all be obvious, though set-mid with /part is something you'll want to play with to make sure you understand it. e.g.
>> s: "the user jsmith logged in at 4.30pm"
== "the user jsmith logged in at 4.30pm"
>> set-mid s "A" 10
== "the user Asmith logged in at 4.30pm" - versus -
>> s: "the user jsmith logged in at 4.30pm"
== "the user jsmith logged in at 4.30pm"
>> set-mid/part s "A" 10 6
== "the user A logged in at 4.30pm" Now, FIND is something else you'll want to play with a bit (the console is great for this), because there are so many refinements. In your case, you might also confuse it with how InStr works in VB, so here's a replacement for that as well (just wrote it here, trying to emulate InStr; test it well): instr: func [ str-1 "Search this" str-2 "look for this" /start pos [integer!] "start looking here" /case "be case sensitive" /local loc ][ pos: any [pos 1] if any [none? str-1 none? str-2] [return none] loc: either case [ find/case at str-1 pos str-2 ][ find at str-1 pos str-2 ] either loc [index? loc][0] ] Now that you have those, you probably won't need them as I see you've gotten a couple answers already. :) PARSE is really great once you get used to it. Happy REBOLing! -- Gregg