Re: Variable names on the fly?
Re: Variable names on the fly?
- Subject: Re: Variable names on the fly?
- From: Paul Berkowitz <email@hidden>
- Date: Mon, 15 Jan 2001 19:21:21 -0800
On 1/15/01 3:39 PM, "as digestion" <email@hidden> wrote:
>
Hi:
>
>
Can someone kindly explain why this doesn't work? I thought I would
>
construct a property name on the fly in order to get the month number, but I
>
get an error. (Can't get...) But if I use a propList like {jug: "1"} and ask
>
for the jug of the propList I always get the correct value, a 1.
>
>
Perhaps it is not possible to construct variable names on the fly?
>
>
-----start
>
set theDate to (date string of (current date))
>
set theMonth to word 2 of theDate
>
display dialog (theMonth) -- "January"
>
>
set monthProps to {mJanuary:"1", mFebruary:"2", mMarch:"3", mApril:"4",
>
mMay:"5", mJune:"6", mJuly:"7", mAugust:"8", mSeptember:"9", mOctober:"10",
>
mNovember:"11", mDecember:"12"}
>
>
set mMonth to ("m" & theMonth)
>
>
display dialog (mMonth) -- "mJanuary"
>
>
set monthNum to mMonth of monthProps --ERROR
>
>
---------stop
>
But you're not constructing a variable name, you're constructing a string.
What you're actually hoping to do is construct a record label, but
mMonth = "mJanuary"
NOT
mJanuary
Since there is no "mJanuary" of monthProps, only an mJanuary, you naturally
get an error.
Because it's a record label, you can't even "run script" on it to get rid of
the string quotes, since the variable as such doesn't exist outside of being
a label of the record monthProps.
You have to do this one of the many other ways. Either way, you can use the
month constants rather than date string and word 2 (which won't work in some
languages). If you have Sigma's Coercions osax, you can just do
set theMonth to (month of (current date) as integer) as string
--"1"
Otherwise, you need a repeat loop:
set theMonth to month of (current date)
set theMonths to {January, February, March, April, May, June, July, August,
September, October, November, December}
repeat with i from 1 to 12
if theMonth = item i of theMonths then
set monthNum to i as string
exit repeat
end if
end repeat
monthNum
--"1"
--
Paul Berkowitz