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: Nigel Garvey <email@hidden>
- Date: Wed, 12 Mar 2003 17:35:04 +0000
Steve Cunningham wrote on Wed, 12 Mar 2003 06:11:11 -0500:
>
>Paul Berkowitz wrote on Tue, 11 Mar 2003 12:51:11 -0800:
Actually, it was me. :-)
>
>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.
>
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?
AppleScript does have to do the equivalent of the loop, but it's done in
one instruction whose underlying code is optimised to check for the
presence of an item. An AppleScript repeat loop consists of individual
high-level instructions for carrying out repeats, incrementing counters,
carrying out tasks, and checking for exit conditions. It's like the
difference between telling someone to walk across the room and giving
them individual, repeated instructions to lift each leg, put it down, and
check if they've reached the wall yet.
In the case of my handler, it turns out that checking that an item's in
the list and then looping until [it's found] is generally faster than
looping until [either it's found or there's nowhere else to look]. The
'in' test is extra but one-off and fast, and it saves at least one exit
condition test each time round the loop.
By the way, my handler's a general one for objects in lists. The other
solutions I've seen here so far all assume that all the items in the list
are text. They're also case-sensitive. If that's what you want, they'll
probably give the best performances.
NG
_______________________________________________
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.