newbie script question re Find command
Peter Brigham MD
pmbrig at gmail.com
Tue Mar 22 18:37:08 EDT 2011
On Mar 22, 2011, at 5:31 PM, Timothy Miller wrote:
> Hiya,
>
> I guess I never tried to write a script like this before. Thought it
> would be simple... Well, it is simple, probably, just not simple in
> a way that I actually understand.
>
> How do I find multiple instances of a string in a given field of a
> given card and record each foundchunk in a variable, and then stop
> repeating the find command when the last instance is found?
>
> I want to do this in the context of a repeat loop that goes to all
> the cards in the stack in sequence.
>
> Sooo, I also need to know... How do I tell the repeat loop to move
> on to the next card:
>
> - if no instance of the string is found on the card
>
> - and
>
> - after the last instance of the string is found on the card
>
> Hope that's clear.
>
> I understand I'll probably want to nest a repeat loop using the find
> command on each card, with exits when certain conditions are met.
> I'm choking on the details.
>
> I don't need a complete and accurate script. Just the general idea.
>
> Thanks in advance.
>
> Tim (the world's oldest newbie)
It's never too late to be a newbie! :-)
Here is a function that returns the all the offsets of a string in a
container:
function offsets tStr,tCntr
-- returns a comma-delimited list of all the offsets of tStr in
tCntr
put "" into mosList
put 0 into startPoint
repeat
put offset(tStr, tCntr,startPoint) into os
if os = 0 then exit repeat
add os to startPoint
put startPoint & "," after mosList
end repeat
if char -1 of mosList = "," then delete last char of mosList
if mosList = "" then return "0"
return mosList
end offsets
Using that, you could first
mark cards where fld <tFld> contains <tStr>
then loop through the marked cards and find the full list of
foundchunks:
put empty into foundList
put length(tStr) into strLength
repeat with c = 1 to the number of marked cards
put the name of marked cd c into cdName
put fld <tFld> of marked cd c into fldContents
put offsets(tStr, fldContents) into offsetList
replace comma with cr in offsetList
repeat for each line f in offsetList
put f & "|" & f+strLength-1 & "|" & cdName & cr after
foundList
-- use pipe char as delimiter,
-- or any other char not found in data
end repeat
end repeat
if char -1 of foundList = cr then delete char -1 of foundList
this will give you the full list of foundchunks in the form:
78|85|card id 1016
266|273|card id 1016
13|20|card id 1018
... etc.
and you can parse this by setting the itemdelim to "|" and referring to
char (item 1 of line n of foundList) to (item 2 of line n of
foundList) of <tFld> of (item 3 of foundList)
to get the chunks.
Not tested, and can probably be streamlined, but you get the idea....
-- Peter
Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig
More information about the use-livecode
mailing list