Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hi there esteemed Monks,
I have a very noob-ish question here regarding refs, though still confounding to me at the very least...
In the code example below, i'm trying to create a hash of arrays:
#!/usr/bin/perl use strict; use warnings; use 5.010; use Data::Dumper; $Data::Dumper::Sortkeys = 1; $Data::Dumper::Terse = 1; $Data::Dumper::Quotekeys = 0; my @a1 = ( 'a1', 1, 1, 1 ); my @a2 = ( 'a2', 2, 2, 2 ); my $a1_ref = \@a1; my $a2_ref = \@a2; my @a = ( $a1_ref, $a2_ref ); my %h = (); for my $i ( 1 .. 2 ) { $h{"$i"} = \@a; } say Dumper \%h;

The Dumper output is
{ '1' => [ [ 'a1', 1, 1, 1 ], [ 'a2', 2, 2, 2 ] ], '2' => $VAR1->{'1'} }

The question here is:
why is $h{'2'} a reference to $h{'1'}? I'm trying to create a hash %h with identical key-values made of the @a array-of-arrays. I want each key-value of the hash to have it's own AoA based on @a, but i'm getting references to $h{'1'} instead. What am i doing wrong??
The Dumper output i'm trying to achieve is:
{ '1' => [ [ 'a1', 1, 1, 1 ], [ 'a2', 2, 2, 2 ] ], '2' => [ [ 'a1', 1, 1, 1 ], [ 'a2', 2, 2, 2 ] ] }
Any help appreciated. thanks in advance!
-dan

In reply to help with references by dw_perlmonks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (6)
    As of 2014-12-26 21:51 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (176 votes), past polls