Re: Get position of item in list
Re: Get position of item in list
- Subject: Re: Get position of item in list
- From: Kai <email@hidden>
- Date: Wed, 25 Jun 2003 20:34:05 +0100
on Tue, 24 Jun 2003 21:53:31 -0500, Joseph Weaks <email@hidden> wrote:
>
These have all been some very wordy solutions.
Hey - we're doin' our best here! (Besides, if my original 6-line, tid-based
handler is "wordy", how would you describe your 9-line loop handler?) ;-)
>
How does one gauge when the simple repeat I'm using is not efficient? A list
>
of 20 items, 50 items, 100 items? Maximum length of each string in the list
>
of 50 characters, 100 characters, 500 characters?
In a word: test.
The previous thread to which I referred earlier included a discussion about
timing comparisons. However, general tests can only offer an indication of
relative performance. If you have specific needs, devise your tests based on
the typical string lengths and list counts that your script is likely to
encounter.
You'll need to use a scripting addition like Jon's Commands (for 'the
ticks') or GetMilliSec. Alternatively, there's Smile's 'Chrono' (for which
I'm sure Emmanuel will be happy to provide further info).
Check out the list archives for the "Timing execution?" thread which
appeared around 7/8 May this year. Also, see Bill Briggs' AppleScript Primer
article at:
http://maccentral.macworld.com/features/applescriptprimer50/
(Obviously, for more specific help on the subject, come back here.)
On the other hand, testing of this kind can be extremely time consuming. So
if you're just looking for a general handler that you can use in various
situations, Paul S's advice makes a lot of sense.
>
on getPosition(theString, theList)
>
if theString is in theList then
>
repeat with x from 1 to (count text items of theList)
>
if (text item x of theList) starts with theString then
>
return x
>
end if
>
end repeat
>
else
>
return 0
>
end if
>
end getPosition
>
>
getPosition("for", {"wan", "too", "tree", "for"})
>
-- 4
>
>
("Starts with" suits my current purpose, only returning the first match.)
(Interestingly, I've noticed before that 'starts with' also seems faster
than 'is' for certain string comparisons.)
In view of your concern about "wordiness", you could condense the above
repeat loop to something like this:
====================
on getPosition(i, l)
if i is not in l then return 0
repeat with n from 1 to (count l)
if l's item n starts with i then return n
end repeat
end getPosition
====================
If you're fairly confident that the list will generally contain the search
string, then you'll get a speed advantage from this form:
====================
on getPosition(i, l)
repeat with n from 1 to (count l)
if l's item n starts with i then return n
end repeat
0
end getPosition
====================
--
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.