Re: Accessing a Record by variable
Re: Accessing a Record by variable
- Subject: Re: Accessing a Record by variable
- From: Bill Cheeseman <email@hidden>
- Date: Tue, 13 Feb 2001 06:06:04 -0500
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
-
Bill Cheeseman, Quechee, Vermont <
mailto:email@hidden>
The AppleScript Sourcebook
<
http://www.AppleScriptSourcebook.com/>
Vermont Recipes-A Cocoa Cookbook
<
http://www.stepwise.com/Articles/VermontRecipes/>