Re: Since Apple Listens to this List... [Case Statements]
Re: Since Apple Listens to this List... [Case Statements]
- Subject: Re: Since Apple Listens to this List... [Case Statements]
- From: has <email@hidden>
- Date: Tue, 12 Feb 2002 00:32:55 +0000
Paul Berkowitz wrote:
>
i don't see why speed is an issue either.
At a guess, a case statement would allow a more efficient implementation
geared to handling many tests. You could maybe hash all the case values at
compile-time and just do a single hash lookup every time the case block is
invoked. With an if...elseif block, AS has to retrieve a value and test it
against another for each line until a match is found, which takes time.
ThePPCGod wrote:
>
Depending on the
>
structural requirements of the case statement (all comparison case entries
>
must be in ascending order if numeric, string, given more than a dozen
>
possible cases to compare to)
I'd hope not: laying all sorts of complex requirements on the user kinda
goes against "ease of use" for a scripting language.
>
A quick example that comes to mind is something that would compare entries in
>
a series of records to 100 possible nation-prefix area codes to determine the
>
time zone offset. Nesting 100 if/elseif statements would be a cludge at best.
Hmmm... not sure I like the thought of a hundred-line case block much more
than a hundred-line if...elseif block. I'd much prefer to use a
hash/pseudo-hash in these sorts of situations. (e.g. See strftimeLib.)
In the case of retrieving information based on, say, 3-digit phone codes,
that's an easy one. I wouldn't even consider using an if...elseif or
switch...case block for something like this - there's other methods that
are much better suited.
Myself, I'd build a thousand-item list, putting the relevant information in
the 'valid' slots and 'missing value' in the rest. All you do then is feed
in the number [get item n of theList] and out comes the info/missing value
(depending on whether the number was valid or not). The list itself could
be constructed at compile-time from whatever source data you like, say by
pulling info from a database or flat text file on disk. Very flexible and
maintainable, with super zippy lookup times.
--
Also, in situations where you must use an if...elseif block, try to put the
most common cases at the top of the block to get the best performance out
of it.
HTH
has
_______________________________________________
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.