a little help with "find" script

Peter M. Brigham, MD pmbrig at gmail.com
Fri Mar 9 22:41:53 EST 2012


Another approach, may not be exactly what you're looking for but can easily be adapted, is a utility handler I've used for years:

function offsets str, at cntr
   -- returns a comma-delimited list of all the offsets of str in cntr
   -- cntr is passed by reference to avoid unnecessary duplication
   -- of very large text blocks in memory
   -- cntr contents are not altered by this function
   if str is not in cntr then return 0
   put "" into offsetList
   put 0 into startPoint
   repeat
      put offset(str,cntr,startPoint) into thisOffset
      if thisOffset = 0 then exit repeat
      add thisOffset to startPoint
      put startPoint & comma after offsetList
   end repeat
   delete last char of offsetList
   return offsetList
end offsets

Very handy in a variety of situations, and quite fast even with large runs of text. For instance I have more than once needed to find, not the first instance of a string in a container, but the *last* instance, which starts with character (item -1 of offsets(str,cntr)) of cntr.

You can also expand this for wordOffsets, lineOffsets, and itemOffsets:

function wordOffsets str, at cntr
   put offsets(str,cntr) into charList
   if charList = 0 then return 0
   put "" into offsetList
   repeat for each item i in charList
      put the number of words of (char 1 to i of cntr) & "," after offsetList
   end repeat
   delete char -1 of offsetList
   return mlo
end wordOffsets

function lineOffsets str, at ctr
  put offsets(str,ctr) into charList
  if charList = "0" then return "0"
  put "" into offsetList
  repeat for each item i in charList
    put the number of lines of (char 1 to i of ctr) & "," after offsetList
  end repeat
  delete char -1 of offsetList
  return mlo
end lineOffsets

function itemOffsets str, at ctr
  put offsets(str,ctr) into charList
  if charList = "0" then return "0"
  put "" into offsetList
  repeat for each item i in charList
    put the number of items of (char 1 to i of ctr) & "," after offsetList
  end repeat
  delete char -1 of offsetList
  return mlo
end itemOffsets

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig

On Mar 9, 2012, at 8:53 PM, Mark Wieder wrote:

> Mark-
> 
> (correcting the typo and...)
> 
> on findit pTextToFind
>    local x, y
>    local tText
>    local tFoundList
> 
>    repeat for each line tCard in the cardnames of this stack
>        repeat with y = 1 to number of fields of card tCard
>            put field y of card tCard into tText
>            if pTextToFind is in tText then
>                put tCard,y & cr after tFoundList
>            end if
>        end repeat
>    end repeat
>    return tFoundList
> end findit
> 
> -- 
> -Mark Wieder
> mwieder at ahsoftware.net
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list