Re: Accessing a Record by variable
Re: Accessing a Record by variable
- Subject: Re: Accessing a Record by variable
- From: email@hidden
- Date: Tue, 13 Feb 2001 13:34:06 EST
Bill,
I rather think you've missed Douglas' point. I think what he's after is the
equivalent of a hash table. Your solution hard codes Feb into the script, and
Douglas wants to look up any arbitrary month's value depending on program
input, as in
--- pseudocode
set listOfMonths to {Jan:"1", Feb:"2", Mar:"3", Apr:"4", May:"5", (*wrap*)
Jun:"6", Jul:"7", Aug:"8", Sep:"9", Oct:"10", Nov:"11", Dec:"12"}
set valueDesired to getValueForArbitraryMonth(Feb)
on getValueForArbitraryMonth(arbitraryMonth)
return arbitraryMonth in listOfMonths
end getValueForArbitraryMonth
-- end pseudocode
I don't think this feature exists in Applescript. Using a loop is slow, and
using the TIDs on massive quantities of information invites memory issues.
IMHO, hash table capabilities in Applescript should be a very high priority,
and the absence is one big reason I am learning Perl.
Jeff Baumann
email@hidden
www.linkedresources.com
History is interesting. An apple fell on Newton's head and he invented
Calculus, while Bill Gates invented Windows and got a pie in the face.
In a message dated 2/13/01 6:56:47 AM, Bill Cheeseman wrote:
>
on 2/12/01 8:02 PM, Douglas Wagner at email@hidden wrote:
>
>
> I'd like to be able to access an AS record using a variable.
>
>
>
> set aMth to "Feb"
>
> set theMths to {Jan:"1", Feb:"2", Mar:"3", Apr:"4", May:"5", Jun:"6",
>
> Jul:"7", Aug:"8", Sep:"9", Oct:"10", Nov:"11", Dec:"12"}
>
> set mthNum to aMth in theMths
>
>
>
> what I'd like to get is --> 2
>
>
>
> I'm astonished this doesn't work. Is there an elegant alternative?
>
>
It's just a matter of three syntax errors. First, you have put Feb in quotes
>
in the first line, so aMth is now a string, not one of the variables in your
>
theMths record. Second, when you fix that by removing the quotes, you will
>
discover that the variable Feb isn't defined yet, because you have tried to
>
access it before you defined it. Third, when you fix that by reversing the
>
order of the first two lines, you will discover that you modified aMth with
>
"in theMnts" in the third line, when you should have done it when you set
>
aMth to Feb.
>
>
Here's a version of your script that works (you'll have to unwrap the first
>
line):
>
>
set theMths to {Jan:"1", Feb:"2", Mar:"3", Apr:"4", May:"5", Jun:"6",
>
Jul:"7", Aug:"8", Sep:"9", Oct:"10", Nov:"11", Dec:"12"}
>
set aMth to Feb of theMths
>
set mthNum to aMth