how to use perl references correctly -


i have noob-ish question here regarding refs, though still confounding me @ least...
in code example below, i'm trying create 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;  @a1 = ( 'a1', 1, 1, 1 ); @a2 = ( 'a2', 2, 2, 2 );  $a1_ref = \@a1; $a2_ref = \@a2;  @a = ( $a1_ref, $a2_ref );  %h = ();  $i ( 1 .. 2 ) {         $h{"$i"} = \@a; }  dumper \%h; 

the dumper output is

{           '1' => [                    [                      'a1',                      1,                      1,                      1                    ],                    [                      'a2',                      2,                      2,                      2                    ]                  ],           '2' => $var1->{'1'}         } 

the question here is:
why $h{'2'} reference $h{'1'}? i'm trying create hash %h identical key-values made of @a array-of-arrays. want each key-value of hash have it's own aoa based on @a, i'm getting references $h{'1'} instead. doing wrong??
dumper output i'm trying achive is:

{           '1' => [                    [                         'a1',                      1,                        1,                        1                       ],                      [                         'a2',                      2,                        2,                        2                       ]                     ],             '2' => [                    [                         'a1',                      1,                        1,                        1                       ],                      [                         'a2',                      2,                        2,                        2                       ]                     ]            }    

any appreciated. in advance!
-dan

it's not $h{'2'} reference $h{'1'}, both references same array, namely @a. want is:

for $i ( 1 .. 2 ) {     $h{"$i"} = $a[$i - 1]; } 

which equivalent this:

$h{'1'} = $a[0];   # i.e., $a1_ref $h{'2'} = $a[1];   # i.e., $a2_ref 

which makes $h{'1'} reference @a1 , $h{'2'} reference @a2.

incidentally, might find helpful use notations [ ... ] , { ... } create references anonymous arrays , hashes (respectively). since never use @a1 , @a2 except via $a1_ref , $a2_ref, might create latter directly:

my $a1_ref = [ 'a1', 1, 1, 1 ];   # reference new array (no name needed) $a2_ref = [ 'a2', 2, 2, 2 ];   # ditto 

edited updated question: copy array, can write:

my @orig = (1, 2, 3); @new = @orig; 

or:

my $orig_ref = [1, 2, 3]; $new_ref = [@$orig_ref]; # arrayref -> array -> list -> array -> arrayref 

in case, if understand correctly, need perform "deep" copy: don't want 2 arrays same elements, want 2 arrays elements references distinct arrays same elements. there's no built-in perl way that, can write loop, or use map function:

my @orig = ([1, 2, 3], [4, 5, 6]); @new = map [@$_], @orig; 

so:

for $i ( 1 .. 2 ) {     $h{"$i"} = [map [@$_], @a]; } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -