A better find and replace function
Robert Sneidar
bobs at twft.com
Thu Sep 28 02:12:05 EDT 2006
This may interest some of you. You may have stumbled across something
with the replace command. Let's say I have a chunk that contains the
word "blah". I want to replace all occurrences of "blah" with
"noblah" which those of you who are astute see contains the word it
replaces. If I call the replace command once, all my "blah"'s become
"noblah"'s okay, but if I execute the command again, my noblahs
become nonoblahs.
What I needed was something that would replace the WORD blah with the
WORD noblah, ignoring words that CONTAIN blah, including subsequent
search and replace executions. In other words I needed a whole word
search and replace that only did it once per occurrence of a whole
word (in the true sense of the "word" ) no matter how many times I
ran it. I mainly need it for renaming function and command calls in a
script, but it could be useful elsewhere.
Just do a wordoffset you say? Oh but nay, because wordoffset will hit
on blah, noblah, blahse etc. I ONLY want the whole word blah, even if
it is a function like say blah(). (Revolution thinks "blah
(somearg,somearg2)" is all one word. :-) It's a lot harder than you
might think, because Revolution's word offset is very liberal when it
comes to what it thinks is a word.
One last note: I employ a technique that I think may also be of
interest. When working with large chunks of text that I am executing
offset functions on I progressively "chop" it down by working with a
copy of the chunk and then on each loop I put the text after what I
already processed into the chunk. So each iteration in the loop I am
working with a progressively smaller chunk. I am not sure what speed
gains I may get, but one added benifit is that when I have dealt with
every occurrence of what I am looking for, the offset function in the
last bit will return 0 and that is my cue to bail from the repeat
loop. Otherwise if I replaced "blah" with "noblah" and did a
wordoffset call on the whole chunk, it would find the "noblah" that I
just created and never exit!!
So here it is, a whole word search and replace function. I am sure
some will find ways to make it better, Have at it.
FUNCTION findreplace thetarget thesearchstring thereplacestring
-- I need a search and replace feature that will not keep
replacing a partial word
-- repeatedly
#breakpoint
put 1 into mfirstline
REPEAT
put line mfirstline to -1 of thetarget into thechunk
-- get the first line containing the search string
put lineoffset(thesearchstring, thechunk) into fline
IF fline is 0 THEN
exit REPEAT
END IF
put line fline of thechunk into theline
-- Check to see that the line is not a comment
IF word 1 of theline is "--" THEN
put mfirstline + fline into mfirstline
next REPEAT
END IF
-- see if this has already been replaced
put word wordoffset(thesearchstring, theline) of theline
into thesearchword
put word wordoffset(thereplacestring, theline) of theline
into thereplaceword
-- thesearchword will not equal thereplaceword IF
thesearchstring IS IN thereplacestring
-- because thereplacestring does not exist in the line
containing thesearchstring
-- BUT WILL if thesearchstring CONTAINS thereplacestring!
It's a gotcha.
IF thesearchword is not thereplaceword OR \
thereplacestring is in thesearchstring THEN
#breakpoint
replace thesearchstring with thereplacestring in theline
put theline into line fline of thechunk
put thechunk into line mfirstline to -1 of thetarget
END IF
put mfirstline + fline into mfirstline
END REPEAT
return thetarget
END findreplace
Bob Sneidar
IT Manager
Logos Management
Calvary Chapel CM
More information about the use-livecode
mailing list