On Fri, 02 Sep 2005 16:09:04 +0900, Mark Sargent wrote:

> Hi All,
> 
> am new to Perl. I'm followig this tutorial,
> 
> http://www.cs.unc.edu/~jbs/resources/perl/perl-basics/variables.html
> 
> and am confused as to why the below shows examples with $ and some with 
> % at the beginning of the statement. When it says, "Perl uses the 
> "percent" symbol and curly braces with respect to the name of an 
> associative array as a whole". Am I to assume that either is fine, when 
> defining associative arrays.? Cheers.

Their examples are correct:

> $aAA{"A"} = 1;  # creates first row of assoc. array
> $aAA{"B"} = 2;  # creates second row of assoc. array
> %bAA = %aAA;  # creates new assoc. array and gives it values of %aAA
> $aAA{"A"} = 3;  # changes the value of first item from 1 to 3
> %aAA = ("A", 1, "B", 2);  # same as first two stmts., above

but the explanation is at best misleading.  Use another tutorial.  Oh, and
we haven't called them "associative arrays" for many years.

You refer to the whole hash with %.  You refer to any member of it with $.
 Why?  The hash is a container; it has its own behavior.  Each item in the
 container is a scalar.  Scalars begin with $ in Perl.  Figure out whether
 you're talking about the container or an item in it and you'll know which
 sigil to use.  Usually you'll use the % once, when declaring the hash:

        my %hash;

and never again, except in the keys() function.  Until you're a bit more
experienced.

-- 
Peter Scott
http://www.perlmedic.com/
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to