[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-AnyPerl] Can Hash Keys Contain Variables?



Quoting Mark Yannuzzi <myannuzzi@aya.yale.edu>:

> I have a hash, %GM, and a counter variable, $cntGM (= 1, 2, etc.). I wish
> to
> construct keys for this hash of the form:
> 
> Min$cntGM, Mean$cntGM, etc. (e.g., Min1, Min2, Max1, Max2, etc.)
> 
> I tried:
> 
> $GM{Min$cntGM} = <some value>;
> 
> since {} are supposed to act as double-quotes, and thus evaluate $cntGM.
> 
> However, when I run my script, I get the following error:
> 
> # Can't call method "Min" without a package or object reference, <INFILE>
> chunk 3102.
> 
The key can be a legal bareword (simple identifier), OR it can be an
expression. As a simple identifier it can have letters, digits and underscores.
Well, actually, the bareword is also an expression.

*If* you want variable interpolation to happen, then explicitly stringify
by using double quotes, as in $MinGM{"Min$cntGM"}.

What's happening with your example is that there's the '$' in there, so it
can't be a bareword. Perl tries to evaluate it as an expression, but why
should it be treated as a string with variables to be interpolated? Perl
doesn't know that unless you indicate as such.

'perldata' says that evaluation of a bareword is as a *quoted* string;
I suspect probably as a *single-quoted* string. Hence no interpolation. The
{} forces the identifier to be a string, also from 'perldata', but again,
I think the quoting is as per single-quoting. And again from 'perldata',
when {} is used, anything more complicated than a simple identifier is
treated as an expression. But your expression is Min$cntGM; NOT "Min$cntGM".

Hope I didn't belabour the point too much. :-)

Arved


---------------------------------------------------------------
 This mail was sent through the Nova Scotia Provincial Server.
 Get your own account: http://nsaccess.ns.ca/

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org