Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

perl hash array printing

by Anonymous Monk
on Sep 04, 2012 at 23:37 UTC ( #991704=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do I get array element 2 of totalhash,hash like this? I looked at a lot of places but i do not really get how to get an element of a hash out of a hash arrayed so to say, I have already done dumping and itirating but I prefer to do it like this. I appreciate the help greatly thank you

print @($totalhash{hello1}{world});

Replies are listed 'Best First'.
Re: perl hash array printing
by choroba (Bishop) on Sep 04, 2012 at 23:50 UTC
    If I understand correctly, you are after
    my $second = $totalhash{hello}{world}[1];
    See Perl Data Structures Cookbook for details.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      yes i believe so only made for array like with @(), I am going to try this out:)

        $ perl -e " use Data::Dump; $totalhash{hello}{world}[1]=9; dd \%total +hash; dd $totalhash{hello}{world}[1]; " { hello => { world => [undef, 9] } } 9

        See References quick reference

Re: perl hash array printing
by Marshall (Abbot) on Sep 05, 2012 at 03:58 UTC
    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' ] } };
Re: perl hash array printing
by Rudolf (Pilgrim) on Sep 05, 2012 at 01:43 UTC

    Hello, hope this helps a bit my example is with an annonymous array though:

    use v5.14; my %hash = ('hello', => ['world','there']); print $hash{'hello'}[1];

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991704]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-04-20 18:45 GMT
Find Nodes?
    Voting Booth?