Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Dereferencing a Hash of Arrays

by toro (Beadle)
on Jun 14, 2011 at 07:33 UTC ( [id://909548]=note: print w/replies, xml ) Need Help??


in reply to Re: Dereferencing a Hash of Arrays
in thread Dereferencing a Hash of Arrays

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.

Replies are listed 'Best First'.
Re^3: Dereferencing a Hash of Arrays
by johngg (Canon) on Jun 14, 2011 at 10:40 UTC

    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-29 08:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found