Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: perl hash array printing

by Marshall (Abbot)
on Sep 05, 2012 at 03:58 UTC ( #991732=note: print w/replies, xml ) Need Help??

in reply to perl hash array printing

Hope that this helps..

#!/usr/bin/perl -w use strict; use Data::Dump qw(pp); use Data::Dumper; my %totalhash; # the value of $totalhash{hello1}{world} is a reference # to an array, a HoHoA $totalhash{hello1}{world} = ["hello world", "hello2", "hello3"]; # I would not do this, ie make a single value instead of a ref to # array... make all of the values ref to array even if there # is only one value for some ...makes the structure more "consistent". # I just show it as a possibility. # $totalhash{diffHello}{world} = "different hello"; print "@{$totalhash{hello1}{world}}\n"; # ** need curly braces # not ()!! #prints: hello world hello2 hello3 # I personally would prefer the arrow notation which makes it # more clear to me that this a reference to array, but you # can leave that out if you wish.. Here this is a minor point. print $totalhash{hello1}{world}->[1],"\n"; #prints: hello2 print pp \%totalhash; print "\n"; # the pp option to Data::Dump is very good, but # since Data::Dump is not a core module and requires installation, # this can be problematic. # Data::Dumper is available on all Perl systems and is "core" print Dumper \%totalhash; __END__ #### All print statements: ### hello world hello2 hello3 hello2 { diffHello => { world => "different hello" }, hello1 => { world => ["hello world", "hello2", "hello3"] }, } $VAR1 = { 'diffHello' => { 'world' => 'different hello' }, 'hello1' => { 'world' => [ 'hello world', 'hello2', 'hello3' ] } };

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991732]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-12-16 15:53 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (455 votes). Check out past polls.