Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991732]
help
Chatterbox?
[Eily]: "tail: warning: following standard input indefinitely is ineffective". I can't help but feel that tail is judging me...
[Your Mother]: Someone had to say it. Better it came from tail.
[LanX]: Cat Mama!
[Your Mother]: Das right.
[Eily]: actually I forgot to provide the filename as a parameter
LanX it reeks.
[Your Mother]: You know what they say, the reeky wheel gets the best price at cheese market.
[Eily]: well I had to google translate that
[Eily]: let me inform you that the Dutch reek translate into the Czech páchnout
[Eily]: very useful default translation :P

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-03-27 16:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.