Seach and replace

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Andycal
Posts: 144
Joined: Mon Apr 10, 2006 3:04 pm

Seach and replace

Post by Andycal » Fri Jun 13, 2008 1:58 pm

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?

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Fri Jun 13, 2008 2:22 pm

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
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

Andycal
Posts: 144
Joined: Mon Apr 10, 2006 3:04 pm

Post by Andycal » Fri Jun 13, 2008 2:39 pm

Good God man! Not a bad bit of cobbling, it works perfectly!!!!!

Wish my programmers worked that fast!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Post by FourthWorld » Fri Jun 13, 2008 3:36 pm

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)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply