Re: Get position of item in list
Re: Get position of item in list
- Subject: Re: Get position of item in list
- From: Richard Morton <email@hidden>
- Date: Wed, 25 Jun 2003 16:33:27 +1000
On Wednesday, June 25, 2003, at 10:46 AM, Kai wrote:
on Tue, 24 Jun 2003 11:46:15 -0400, Paul Skinner wrote:
This one will (like Kai's) uses the TIDs to find the item. It also
returns ALL of the occurrences of the search term...
on itemIndex(parameters)
[...]
end itemIndex
This (faster) alternative is an all-occurrence variation on my original
suggestion:
==============================
property d : ASCII character 1
to getPosition of i from l
if i is not in l then return 0
Kai may be aware of this already, but seeing as it's speed we're
considering, it's perhaps worth noting that assigning multiple values
in one statement:
set {n, o, t, text item delimiters} to {0, {}, text item delimiters, d
& d}
Is slower than doing them separately:
set n to 0
set o to {}
set t to text item delimiters
set text item delimiters to d & d
Though it doesn't look as cool.
set {l, text item delimiters} to {d & l & d, d & i & d}
set {l, text item delimiters} to {l's text items 1 thru -2, d & d}
repeat with i in l
set i to i as string
if i is not "" then set n to n + 1
tell n + (count i's text items) to set {n, o's end} to {it, it}
end repeat
set text item delimiters to t
o
end getPosition
set txtList to {"nine", "six", "six", "two", "sixteen", "sixty", "six"}
getPosition of "six" from txtList
--> {2, 3, 7}
FWIW, redoing it with all separate statements:
to getPosition of i from l
if i is not in l then return 0
set n to 0
set o to {}
set t to text item delimiters
set text item delimiters to d & d
set l to d & l & d
set text item delimiters to d & i & d
set l to l's text items 1 thru -2
set text item delimiters to d & d
repeat with i in l
set i to i as string
if i is not "" then set n to n + 1
tell n + (count i's text items)
set n to it
set o's end to it
end tell
end repeat
set text item delimiters to t
return o
end getPosition
And running it like this:
set txtList to {"nine", "six", "six", "two", "sixteen", "sixty", "six"}
set sta to GetMilliSec -- GetMilliSec osax
repeat 50 times
getPosition of "six" from txtList
end repeat
(GetMilliSec) - sta
Shows an average 10% decrease in execution time. G4/400, OS10.2, AS
1.9. Anyone who wants to speed test it under classic MacOS can use the
Precision Timer osax.
Cheers,
Count Items
-- If you need to load or unload, go to the White Zone
_______________________________________________
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.