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: Paul Skinner <email@hidden>
- Date: Wed, 12 Mar 2003 11:17:45 -0500
On Wednesday, March 12, 2003, at 06:11 AM, Steve Cunningham wrote:
Paul Berkowitz wrote on Tue, 11 Mar 2003 12:51:11 -0800:
<SNIP>
If the list is quite large, you can usually speed up the process by
using
a binary search, though there are positions in all lists that a
straight-through loop will find more quickly. Here's a sketch from a
project I shelved last year. If you need case sensitivity, put the
call
to the handler in a 'considering case' block.
<SNIP>
Thanks, I'll give it a try. One question though...
if theList does not contain searchItem then return 0
I have used this technique myself, ie why search for the item if it is
not in the list to start with (terrible grammar, but you know what I
mean), but I always have the nagging suspicion that in fact I may be
searching the list twice by doing this and not saving any time :-).
That
is, to determine if the item is in the list, AppleScript has to do the
equivalent of my repeat loop, so why not just do it once and be done
with
it?
Can anyone comment on the truth or falsity of this?
Steve
You could time the 'contains' running against a big list (3000 items)
and see how it performs when the search term is first (0.0034) and when
it is last (0.2273). Now compare it's speed to just looping from 1 to
3000 (.00002). It seems to be iterating through the list in some way.
If you wanted you could break the text with the search term, by using
the TIDs [1], and just count the pieces. One instance would yield two
pieces. Speed is fairly linear and quick. first item matches (0.0027)
last item matches (0.0495) with 3000 items. Text to list coercion
appears to be a factor in it's linearity.
set t to read file ((choose file) as string)
set t to words of t
ListContains(t, "script")
on ListContains(inputList, searchTerm)
set AppleScript's text item delimiters to "o#?"
set inputList to "o#?" & inputList & "o#?"
set AppleScript's text item delimiters to "o#?" & searchTerm & "o#?"
try
return (length of (inputList's text items) is greater than 1)
on error e number n
if n is not -2706 then
error e number n
else
return true
end if
end try
end ListContains
[1] No disclaimer on this one. It handles any number of occurrences of
the search term.
Paul Skinner
Only Irish coffee provides in a single glass all four essential food
groups: alcohol, caffeine, sugar, and fat.
-Alex Levine
_______________________________________________
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.