finding repeating patterns

Peter Brigham pmbrig at comcast.net
Mon Aug 28 10:05:16 EDT 2006


On Sun, 27 Aug 2006 10:00:24 -0700 Rob Cozens <rcozens at hidden> wrote:

****
This started out as a mental exercise for moi, and the mouseUp logic
is untested:

...<snip>...

OTOH, working on it led me to the following offsets function, which
has been tested.

function offsets targetString, sourceString
    put empty into offsetsList
    put 0 into offsetAdjustment
    put length(targetString)-1 into targetLengthAdjustment
    repeat
      get offset(targetString,sourceString)
      if it = 0 then return offsetsList
      put (it+offsetAdjustment) &return after offsetsList
      put it+targetLengthAdjustment into deleteCutoff
      delete char 1 to deleteCutoff of sourceString
      add deleteCutoff to offsetAdjustment
    end repeat
end offsets

Example: offsets("at","The cat in the hat smelled a rat where he  
sat.") returns

6
17
31
44

Note that the same logic can be applied to create lineOffsets,
itemOffsets, and wordOffsets functions.

Enjoy!
****

Here's my version of the same thing -- I use it constantly.

****************

function multOffset str,ctr
   -- returns a comma-delimited list of all the offsets of str in ctr
   put "" into mosList
   put 0 into startPoint
   repeat
     put offset(str,ctr,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 char -1 of mosList
   return mosList
end multOffset

function multLineOffset str,ctr
   -- returns a comma-delimited list of all the lineOffsets of str in  
ctr
   put multOffset(str,ctr) into charList
   if charList = "0" then return "0"
   put the number of items of charList into nbr
   put "" into mlo
   repeat with n = 1 to nbr
     put the number of lines of (char 1 to (item n of charList) of  
ctr) & "," after mlo
   end repeat
   if char -1 of mlo = "," then delete char -1 of mlo
   return mlo
end multLineOffset

****************

-- Peter

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

~+~+~+~+~+~+~+~+~+~+~+~+~+~+~+~
My life has a superb cast, but I can't figure out the plot.




More information about the use-livecode mailing list