Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: perl hash array printing

by Marshall (Prior)
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' ] } };


Comment on Re: perl hash array printing
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (13)
As of 2014-07-24 20:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (166 votes), past polls