Re: Determining item number matching "x" in a list
Re: Determining item number matching "x" in a list
- Subject: Re: Determining item number matching "x" in a list
- From: Steve Cunningham <email@hidden>
- Date: Thu, 13 Mar 2003 14:31:14 -0500
Emmanuel <email@hidden> wrote:
>
Subject: Re: Determining item number matching "x" in a list
>
>
At 3:12 PM -0700 11/03/03, Ivan Andrus wrote:
>
>
>
>offset of "b" in (theList as text) / length_of_each_item
>
>
Pushing further Ivan's suggestion, and as by coincidence I just
>
needed such a handler today, I submit below a plain-vanilla short
>
handler that retrieves the rank of a given item in a list of strings.
>
The strings should not contain a Carriage Return character. The
>
handler will retrieve only the first hit if there are several.
>
>
------------------------------
>
on IndexOfItem(theItem, theList)
>
set {savedTID, text item delimiters} to {text item delimiters, return}
>
set theList to return & theList & return
>
set text item delimiters to savedTID
>
set theOffset to offset of (return & theItem & return) in theList
>
if theOffset is 0 then return 0
>
-1 + (count paragraphs of (text 1 thru theOffset of theList))
>
end IndexOfItem
>
>
IndexOfItem("Bastia", {"AC Ajaccio", "Auxerre", "Bastia", "Bordeaux"})
>
--> 3
>
IndexOfItem("Basti", {"AC Ajaccio", "Auxerre", "Bastia", "Bordeaux"})
>
--> 0
>
------------------------------
>
>
There is no built-in size limit that I know, but I did not test much
>
(I'm using it with 20 items ...)
That *is* pretty slick. I have learned a lot just looking at everyone's
different approaches :-). This would seem to be such a common
requirement, that one would think someone had written an osax to do it.
I am working on a script that will be executed when Claris Emailer (I
know, I know, so I don't want to hear it :-)) receives an incomming
email. I need it to be fast so Emailer is ready to get the next message
without a long delay.
I have written the very unimaginative, brute force, and plain vanilla
script:
on getIndex(theItem, theList)
set theIndex to 0
repeat with nn from 1 to count of theList
if theItem = item nn of theList then
set theIndex to nn
exit repeat
end if
end repeat
return theIndex
end getIndex
I ran this 500 times and Emmanuel's script 500 times for the same item
and the same list.
Mine completes in 242 ms and Emmanuel's in 1385 ms (on my Lombard).
Although more elegant and creative, I conclude there is a performance
price for the convenience of using things like "offset" and "paragraph".
So here is my challenge: what is the *fastest* way to do this job?
Steve
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.