Re: Translate a code of 2 characters to a text
Re: Translate a code of 2 characters to a text
- Subject: Re: Translate a code of 2 characters to a text
- From: has <email@hidden>
- Date: Tue, 7 May 2002 20:40:01 +0100
Matthew Stuckwisch wrote:
>
I think it could be done even quicker...
>
>
-----
>
on what_is(abbrev)
[...]
>
end what_is
>
------
Indeedily doodily. Spot on. [The keys are all equal length, so calculating
list index is a breeze.] It's case sensitive, but I'm guessing this is not
an issue here.
You can make it even faster if you avoid the osax call by using TIDs instead:
======================================================================
property keyString : "CD SA MD MC VH DV "
property valueList : {"COMPACT DISC", "SUPER AUDIO", "MINIDISC",
[NO-BREAK]"MINICASSETTE", "VIDEO", "DIGITAL VERSATILE DISC"}
on what_is(abbrev)
set oldTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to abbrev
set listIndex to (keyString's first text item's length) / 3 + 1
set AppleScript's text item delimiters to oldTID
try
return item listIndex of valueList
on error number -1728
error "Not Found."
end try
end what_is
======================================================================
I seem to recall posting something similar not that long ago which handled
variable length keys, but have forgotten where. In truth, it was just a
simplified rehash of my existing associativeArrayLib.
BTW, this exercise gets even simpler if you use associativeArrayLib:
======================================================================
property ListOfMedia : {{"CD", "COMPACT DISC"}, {"SA", "SUPER
[NO-BREAK]AUDIO"}, {"MD", "MINI DISC"}, {"MC", "MUSICASSETTE"},
[NO-BREAK]{"VH", "VIDEO"}, {"DV", "DIGITAL VERSATILE DISC"}}
--load and init the array object at compile-time for speed
property theArray : newAssociative({importList:ListOfMedia}) of (load
[NO-BREAK]script "[your path to associativeArrayLib here]")
--
tell theArray to getValue("VH")
--> "VIDEO"
======================================================================
But in this case, where the keys are all equal length, the custom handler
is faster (plus you don't have to worry about library dependencies).
(Though what we'd really like is native hash arrays in AS to begin with...;p)
has
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
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.