Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Difference in Hash Declaration

by MidLifeXis (Monsignor)
on May 07, 2012 at 13:47 UTC ( #969262=note: print w/ replies, xml ) Need Help??


in reply to Difference in Hash Declaration

The only one that is syntactically valid is the second one, however, I don't believe that it is doing what you are expecting. Since the parens denote precedence, what Perl reads it as is:

my %sham = ( 1 => "j", 2 => "b", 1 => "p", 2 => "b", );

When assigning to hashes, later insertions with the same key end up replacing any earlier entries with the same key, leaving you with:

my %sham = ( 1 => "p", 2 => "b", );

This can be shown with the use of Data::Dumper or the like. I think what you are intending is:

my @sham = ( # note the sigil { 1 => "j", 2 => "b", }, { 1 => "p", 2 => "b", }, );

Data::Dumper (and any similar module) is a useful tool for visualizing nested data structures.

--MidLifeXis


Comment on Re: Difference in Hash Declaration
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://969262]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2015-07-07 01:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls