Page 1 of 1

Seach and replace

Posted: Fri Jun 13, 2008 1:58 pm
by Andycal
I'm an occasional user of Runrev being a PHP programmer by trade, but I've been using a PHP routine for a while now that seems to lend itself perfectly to RunRev, but I've no idea how to start at it.

I've been using a function that goes through a bunch of text that has been pre formatted like this:

Code: Select all

{Greetings|Hello} and welcome to the {most adventurous|greatest} show you will ever see {in the world|on this good earth|in the universe}!
What the routine does is when it meets a set of curly brackets, it takes one of the phrases (seperated with the '|') and uses it, discarding the other. So, from the above phrase, multiple unique phrases can be generated.

Is this easy enough to do in RunRev?

Posted: Fri Jun 13, 2008 2:22 pm
by BvG
Rev lends itself more to single seperator for one type of thing, for example tab delimited lists of stuff. Therefore "{}" and then adding "|" on top, will be a bit hard to work with. So ideally you'd have a way that only uses two signs instead of the mentioned three.

Barring that, I cobbled this together, based on how I parse xml or html stuff. I'm sure there's more efficient and/or niftier ways:

Code: Select all

on mouseUp
  set the linedelimiter to "}"
  set the itemdelimiter to "{"
  put field 1 into theData
  put "" into theResult
  repeat for each line theLine in theData
    if item -1 of theLine contains "|" then
      put item -1 of theLine into theChoices
      put item 1 to -2 of theLine after theResult
      set the itemdelimiter to "|" 
      put any item of theChoices after theResult
      set the itemdelimiter to "{"
    else
      put theLine after theResult
    end if
  end repeat
  put theResult
end mouseUp

Posted: Fri Jun 13, 2008 2:39 pm
by Andycal
Good God man! Not a bad bit of cobbling, it works perfectly!!!!!

Wish my programmers worked that fast!

Posted: Fri Jun 13, 2008 3:36 pm
by FourthWorld
Rev has a merge function which will return the evaluation of any valid expression within double brackets.

So you could write your template as
[[any item of "Greetings,Hello"]] and welcome to the [[ any item of "most adventurous,greatest"]] show you will ever see [[any item of "in the world,on this good earth,in the universe"]]!
...and if for example you have that in a variable named tMyData you could process it with:
return merge(tMyData)