Search 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

Search and replace

Post by Andycal » Wed Jun 10, 2009 12:41 pm

Hiya all,

I'm convinced (I might be very wrong though) that I've seen an example somewhere of a multiple search and replace thing. There was a text box with some content and another with some words like this:

white, black
red, green

And it would go through and replace all instances with the first words with the second.

Anyone seen this? I've been through loads of examples and I'm probably missing it somewhere.

Andy.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Jun 10, 2009 1:30 pm

Hi Andy,

I'm not sure what you're after. Perhaps this:

put replaceText(fld 1,"red","green") into fld 1

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Post by Andycal » Wed Jun 10, 2009 1:35 pm

Hi Mark,

That's the gist of it, however the demo I saw allowed for multiple seach and replace, but while thinking about it, I think it's pretty easy anyway.

Correct me if I'm wrong, but I just do a 'for each tLine in fld 'Replacements' and then split on the comma, and replace the first word with the second word.

The whole idea is that I've got a big field of text with lots of abbreviations in it and I want to replace all the abbreviations. I've got another field with the abbreviation and the full word, comma seperated, each one on a seperate line.

You know what, it's difficult to explain, I should go and try it out!

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Jun 10, 2009 2:31 pm

Andy,

I don't understand you! The replace function replaces all instances of a string. What else do you want?!

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Post by Andycal » Wed Jun 10, 2009 3:02 pm

Told you it was confusing!

Let's say I've got the following paragraph:
"Outpt rose by 0.3% from the prev mnth the Off. for National Stats said. Analysts had been expecting outpt to fall by 0.1%

Mnf rose 0.2%, which was also an unexpected rise."
Now, in this, plus other documents the same abbreviations are used in the same way, so I want to turn the abbreviations into the full words. I have a list of the abbreviations and full words as a comma seperated file:

Outpt, Output
prev, previous
Off., Office
Stats, Statistics

So I have one text box with the paragraph in that contains a lot of abberviated words and I have another text box with the comma seperated list as above. I then press a button and it goes through all of them and replaces each occurance.

Does that make sense?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Wed Jun 10, 2009 3:21 pm

Hi Andy,

What you want is this:

Code: Select all

put fld "Abbrevs" into myAbbrevs
replace comma & space with comma in myAbbrevs
lock screen
repeat for each line myAbbr in myAbbrevs
  replace item 1 of myAbbr with item 2 of myAbbr in fld "Paragraph"
end repeat
unlock screen
I believe this is almost the same as what you describe in your previous message.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

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

Post by Andycal » Wed Jun 10, 2009 3:33 pm

I was just about to post this:

Code: Select all

on mouseUp
   set the columndelimiter to comma
   repeat for each line tLine in fld "fldSearch"
      put word one of tLine into tFind
      replace "," with "" in tFind
      put word two of tLine into tReplace
      replace tFind with tReplace in fld "fldText"
   end repeat
         answer "All done! " with "OK"
end mouseUp
Yours looks much more elegant!

I'll go try it...

Post Reply