• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: similar strings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: similar strings


  • Subject: Re: similar strings
  • From: "Nigel Garvey" <email@hidden>
  • Date: Tue, 10 Jan 2006 22:37:38 +0000

kai wrote on Tue, 10 Jan 2006 14:41:48 +0000:

>set s to "The answer on is now is the time for a party
>with foxes and cows but no good men"
>set l to "Now is the time for all good men to come to the
>aid of the party while the quick brown fox jumps over the lazy dog.
>How now brown cows but no good men"
>
>set v to 1
>set b to 1
>set e to 1
>set r to {}
>set c to (count l) + 1
>repeat
>	repeat while e < c and l's text b thru e is not in s
>		set e to e + 1
>		set b to b + 1
>	end repeat
>	repeat while e < c and l's text b thru e is in s
>		set r's end to b
>		set e to e + 1
>	end repeat
>	tell e - b to if it > v then
>		set v to it
>		set r to {b}
>	end if
>	if e is c then
>		if (count r) is 0 then return {0, ""}
>		repeat with i in r
>			set i's contents to l's text i thru (i + v - 1)
>		end repeat
>		return {v} & r
>	end if
>	set b to b + 1
>end repeat
>
>--> {21, "Now is the time for a", " cows but no good men"}

And here's a version for those who prefer vaguely meaningful variable
names and no nested repeats:  :-)

  set shortStr to "The answer on is now is the time for a party with
foxes and cows but no good men"
  set longStr to "Now is the time for all good men to come to the aid of
the party while the quick brown fox jumps over the lazy dog. How now
brown cows but no good men"

  set maxMatchLength to 0
  set match to false
  set matchOffsets to {}
  set b to 1

  repeat with e from 1 to (count longStr)
    if (longStr's text b thru e is in shortStr) then
      set matchOffsets's end to b
      set match to true
    else if (match) then
      set match to false
      set matchLength to e - b
      if (matchLength > maxMatchLength) then
        set maxMatchLength to matchLength
        set matchOffsets to {b}
      end if
      set b to b + 2
    else
      set b to b + 1
    end if
  end repeat

  if (match) and (e - b + 1 > maxMatchLength) then
    set maxMatchLength to e - b + 1
    set matchOffsets to {b}
  end if

  if (maxMatchLength is 0) then return {0, ""}
  repeat with thisOffset in matchOffsets
    set thisOffset's contents to longStr's text thisOffset thru
(thisOffset + maxMatchLength - 1)
  end repeat
  return {maxMatchLength} & matchOffsets

  --> {21, "Now is the time for a", " cows but no good men"}


While experimenting with the variable names, I tried 'matchLen', which
compiled as a keyword. Anyone know what might be claiming it?

NG

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: similar strings
      • From: kai <email@hidden>
  • Prev by Date: Server login
  • Next by Date: Re: similar strings
  • Previous by thread: Re: similar strings
  • Next by thread: Re: similar strings
  • Index(es):
    • Date
    • Thread