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: Kai <email@hidden>
- Date: Sat, 15 Mar 2003 02:12:49 +0000
on Thu, 13 Mar 2003 23:41:58 +0100, Emmanuel <email@hidden> wrote:
>
If the item is 6th or below, "getindex" is better, for the 7th or
>
above "IndexOfItem" is faster.
>
>
(You improve significantly IndexOfItem's performances if you can
>
forget about resetting tids).
>
>
Logically, thus, a faster algorithm would be hybrid: you would use
>
the loop method to test whether the item is in the very beginning of
>
the list, then you would fire the string method on the rest of the
>
list. With the short list I used (20 items), the additional lines of
>
script required to joint the two methods make the hybrid algorithm
>
ineffective.
It's probably also worth bearing in mind that the times taken to process
shorter lists are less significant. (I guess one practical reason for
playing around with different algorithms is to try and reduce the more
noticeable delays in a script.)
>
My faster proposition so far (10% better than previously):
>
>
on IndexOfItem(theItem, theList)
>
set text item delimiters to return
>
set theList to return & theList & return
>
try
>
-1 + (count (paragraphs of (text 1 thru (offset of
>
(return & theItem & return) in theList) of theList)))
>
on error
>
0
>
end try
>
end IndexOfItem
OMM, this offers an improvement on short-medium length string lists - as
well as avoiding the restriction on processing text containing return
characters. (I also felt more comfortable resetting tids):
=======================
to getIndex of i from l
set d to "}" -- any character/string unlikely to appear in l [1]
set text item delimiters to d
set {x, text item delimiters} to {d & l & d, d & i & d}
set {x, text item delimiters} to {x's text item 1, d}
set {o, text item delimiters} to {count (x's text items), {""}}
if o > (count l) then return 0
o
end getIndex
=======================
[1] The character shown here will undoubtedly have been mangled by the list
server.
--
Kai
_______________________________________________
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.