Re: Choose from list
Re: Choose from list
- Subject: Re: Choose from list
- From: Stephen Swift <email@hidden>
- Date: Tue, 07 May 2002 20:09:14 -0400
At 5/6/02 1:27 PM, Arthur J Knapp (email@hidden) Wrote:
>
> I love choose from list because it offers more options instead of the 3
>
> button alternative. However, my problem arises when the script has to
>
> determine what option the user selected and what course of action to take
>
> from there.
>
>
> ... (*Speed Freak Alert*) ...
>
>
Well, that caught my attention... ;-)
I thought it might. :-)
>
>
>
> ... Sure, there is the classic if then
>
> statement but is it the most efficient, the fastest? Could I instead use
>
> some sort of repeat loop or something?
>
>
on StrIndex(i)
>
set s to return
>
repeat i times
>
set s to s & space
>
end repeat
>
end StrIndex
>
>
set a to {"Item 1" & StrIndex(1), "Item 2" & StrIndex(2),
>
"Item 3" & StrIndex(3), "Item 4" & StrIndex(4)}
Cool. However, doesn't this require the script to calculate every one of
the handlers? Would I see a drop in speed when I had a list of 50 or 100?
>
>
choose from list a
>
>
set s to item 1 of result --> cfl returns a list
>
>
set x to length of paragraph -1 of s --> offset of selected item in a
>
>
>
Several osaxen offer list-index commands. You can check out this:
>
>
<http://osaxen.com/index.php>
>
>
Several scripters have also implemented vanilla search techniques that are
>
faster than a simple traversal. Here is a basic search technique:
>
>
(* Binary "Contains" Search:
>
*)
>
on BCSearch(a, v)
>
tell a to if it contains {v} then
>
set l to 1
>
set r to length
>
repeat until item l = v
>
set m to (l + r) div 2
>
if items l thru m contains {v} then
>
set r to m
>
else
>
set l to m + 1
>
end if
>
end repeat
>
return l
>
end if
>
return 0
>
end BCSearch
This is awesome! This is my new favorite way to generate item #'s! Thanks.
Are there any advantages/disadvantages in using this method (BCSearch) vs.
StrIndex? With a little tweaking, they do basically the same thing. Is it
just a matter of preference?
>
>
>
In certain situations, we can take advantage of the specific type of data that
>
we are searching. For instance, every item of your example list is a string of
>
the same length:
Very good point. I'll remember to look out for little things like that.
Stephen Swift
email@hidden
-----------------------------------------------
AppleScript Guru - The Mac Observer
http://www.macobserver.com/tips/applescript
_______________________________________________
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.