• 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: kai <email@hidden>
  • Date: Tue, 10 Jan 2006 05:18:42 +0000


On 10 Jan 2006, at 02:38, deivy petrescu wrote:

On Jan 9, 2006, at 16:26, deivy petrescu wrote:

Correct my script to:

Ok, final version:

Nice one, Deivy. :-)

I just couldn't resist this variation:

--------

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 cow"

set v to 0
set b to 1
set c to (count l) + 1
repeat
	set e to b + v
	repeat while e < c and l's text b thru e is in s
		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 v is 0 then return {0, ""}
		return {v, l's text r thru (r + v - 1)}
	end if
	set b to b + 1
end repeat

--> {21, "Now is the time for a"}

--------

The script has some problems, namely, it does not handle the case of two different strings of the same length as being maximal. The script handles this by using using a hierarchy, if you find two different strings of the same length then the one from left to right has priority over the right to left. If you want to change the order then change the if lines with tf (text forward) and tb (backwards).

This should list any strings that share the maximum length:

--------

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 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, ""}
		tell r to repeat with i in it
			set i's contents to l's text i thru (i + v - 1)
		end repeat
		return {v} & r
	end if
	set e to b + v
	set b to b + 1
end repeat

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

--------

---
kai


_______________________________________________ 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>
References: 
 >Re: similar strings (From: Barry Wainwright <email@hidden>)
 >Re: similar strings (From: Feat <email@hidden>)
 >Re: similar strings (From: deivy petrescu <email@hidden>)
 >Re: similar strings (From: deivy petrescu <email@hidden>)

  • Prev by Date: Re: Substituting a variable in UI scripting
  • Next by Date: Re: display alert parameters
  • Previous by thread: Re: similar strings
  • Next by thread: Re: similar strings
  • Index(es):
    • Date
    • Thread