in reply to Dereferencing a Hash of Arrays

You have two problems:

my %alphabet = \( 'a' => @atags, 'b' => @btags, );

You are using a list reference which produces a list of references, so your hash is the same as:

my %alphabet = ( \'a' => \@atags, \'b' => \@btags, );


say for @{values %alphabet};

The construct @{} dereferences an array reference but values %alphabet does not return an array reference, it returns a list.

Replies are listed 'Best First'.
Re^2: Dereferencing a Hash of Arrays
by toro (Beadle) on Jun 14, 2011 at 07:33 UTC

    input:

    my @atags = qw( 1 2 3 4 ); my @btags = qw( 9 8 7 6 ); my %alphabet = ( 'a' => \@atags, 'b' => \@btags, ); say for values %alphabet;

    output:

    ARRAY(0x8376dd8) ARRAY(0x837f298)

    UPDATE: I did say for values %alphabet in response to an earlier version of Re^1 without the @{...}. My original code was my %alphabet = ('a', \@atags, 'b', \@btags); say for @{values %alphabet}; which prints an error similar to the OP.

      The "values" are array references so you have to dereference them using @{ ... } to get the array elements. You can also get there using keys and dereferencing the looked up value in the hash.

      knoppix@Microknoppix:~$ perl -MData::Dumper -Mstrict -wE ' > my @aTags = ( 1 .. 4 ); > my @bTags = reverse 6 .. 9; > my %alpha = ( a => \ @aTags, b => \ @bTags ); > print Data::Dumper->Dumpxs( [ \ %alpha ], [ qw{ *alpha } ] ); > say q{-} x 20; > say qq{@{ $_ }} for values %alpha; > say q{-} x 20; > say qq{@{ $alpha{ $_ } }} for keys %alpha; > say q{-} x 20; > foreach my $arrayRef ( values %alpha ) > { > say for @{ $arrayRef }; > say q{-} x 20; > } > foreach my $key ( keys %alpha ) > { > say for @{ $alpha{ $key } }; > say q{-} x 20; > }' %alpha = ( 'a' => [ 1, 2, 3, 4 ], 'b' => [ 9, 8, 7, 6 ] ); -------------------- 1 2 3 4 9 8 7 6 -------------------- 1 2 3 4 9 8 7 6 -------------------- 1 2 3 4 -------------------- 9 8 7 6 -------------------- 1 2 3 4 -------------------- 9 8 7 6 -------------------- knoppix@Microknoppix:~$

      I hope this is helpful.

      Update: Expanded the example code to show how to print one element per line as the OP's code seemed to want.

      Cheers,

      JohnGG