Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to access Hash of hashes with multi-dimensional Array

by IamAwesom3 (Initiate)
on Apr 18, 2013 at 21:23 UTC ( #1029421=perlquestion: print w/ replies, xml ) Need Help??
IamAwesom3 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I am trying to access multi dimensional array in hash. but I am not able to print the values seperately.

my ($key2,$item,$hash,$array); my %temp = ( "0x55555555" => { "0x55555555" => [ ["0xAAAAAAAA", "0x9"], ], "0xAAAAAAAA" => [ ["0xAAAAAAAA", "0x8"], ], }, "0xAAAAAAAA" => { "0x55555555" => [ ["0xFFFFFFFF", "0x8"], ], "0xAAAAAAAA" => [ ["0x55555554", "0x3"], ], }, ); foreach $item (keys %temp) { print "$item: \n"; my $hash = $temp->{$item}; foreach $key2 (keys %$hash){ my $array = $hash->{$key2}; for $i (0..$#$array){ print "$i: $$array[$i]\n"; } } }
Output I get is
0xAAAAAAAA: 0x55555555:

I want to access each and every element of the hash, How can I do that? Please help me. If you can explain it with a note then that will be great.

Thanks P

Comment on How to access Hash of hashes with multi-dimensional Array
Select or Download Code
Re: How to access Hash of hashes with multi-dimensional Array
by tobyink (Abbot) on Apr 18, 2013 at 21:41 UTC
    use strict;

    Global symbol "$temp" requires explicit package name

    Note that %temp and $temp are different variables. $temp{foo} is used to access values in %temp; $temp->{foo} is used to access values in $temp.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: How to access Hash of hashes with multi-dimensional Array
by 2teez (Priest) on Apr 18, 2013 at 22:45 UTC

    "..I want to access each and every element of the hash, How can I do that?.."
    You can do like so:

    use warnings; use strict; my %temp = ( "0x55555555" => { "0x55555555" => [ [ "0xAAAAAAAA", "0x9" ], ], "0xAAAAAAAA" => [ [ "0xAAAAAAAA", "0x8" ], ], }, "0xAAAAAAAA" => { "0x55555555" => [ [ "0xFFFFFFFF", "0x8" ], ], "0xAAAAAAAA" => [ [ "0x55555554", "0x3" ], ], }, ); foreach my $item ( keys %temp ) { print "$item: \n"; # print keys my $hash = $temp{$item}; foreach my $key2 ( keys %$hash ) { print "\t", $key2, "\n\t"; # print keys my $array = $hash->{$key2}; print "\t", join "\t", map { @$_ } @$array; # print all you +r values print "\n"; } }
    OUTPUT:
    0xAAAAAAAA: 0xAAAAAAAA 0x55555554 0x3 0x55555555 0xFFFFFFFF 0x8 0x55555555: 0xAAAAAAAA 0xAAAAAAAA 0x8 0x55555555 0xAAAAAAAA 0x9
    Hope this helps.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      Thanks it did work but I want to use each element of array separately. like for example

      $temp{$hash}[0][1]
      how do I do that? I looked at the debugger option suggested by Tom. it did help but I am not able to write it in perl.

      and what will -> do in this code? my $array = $hash->{$key2};

      Thanks

      P

Re: How to access Hash of hashes with multi-dimensional Array
by TomDLux (Vicar) on Apr 19, 2013 at 01:31 UTC

    If you have a problem figuring out how to deal with data structures, run the program in the perl debugger, then figure out bits one elevel at a time. I used a simpler structure than yours, cause copying it was work. ( -d is the debugger flag. if you don't specify a file, you need to specify an immediate expression, -e. 'mo' is meaningless garbage which generates an error, but makes for a useful mnemonic)

    legrady$ perl -demo Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): mo DB<1> %temp = ( '0x55555555' => { '0x55555555' => [["0xAAAAAAAA", "0 +x9"]]}); DB<2> x \%temp 0 HASH(0x7fa2811ab830) '0x55555555' => HASH(0x7fa2811ab920) '0x55555555' => ARRAY(0x7fa2811ab8d8) 0 ARRAY(0x7fa281028b78) 0 '0xAAAAAAAA' 1 '0x9' DB<3> x $temp{'0x55555555'} 0 HASH(0x7fa2811ab920) '0x55555555' => ARRAY(0x7fa2811ab8d8) 0 ARRAY(0x7fa281028b78) 0 '0xAAAAAAAA' 1 '0x9' DB<4> x $temp{'0x55555555'}{'0x55555555'} 0 ARRAY(0x7fa2811ab8d8) 0 ARRAY(0x7fa281028b78) 0 '0xAAAAAAAA' 1 '0x9' DB<5> x $temp{'0x55555555'}{'0x55555555'}[0] 0 ARRAY(0x7fa281028b78) 0 '0xAAAAAAAA' 1 '0x9' DB<6> x $temp{'0x55555555'}{'0x55555555'}[0][0] 0 '0xAAAAAAAA' DB<7> x $temp{'0x55555555'}{'0x55555555'}[0][1] 0 '0x9' DB<8> q

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Re: How to access Hash of hashes with multi-dimensional Array
by Anonymous Monk on Apr 19, 2013 at 01:54 UTC

    What about using the map function in Perl:

    use warnings; use strict; my %temp = ( "0x55555555" => { "0x55555555" => [ [ "0xAAAAAAAA", "0x9" ], ], "0xAAAAAAAA" => [ [ "0xAAAAAAAA", "0x8" ], ], }, "0xAAAAAAAA" => { "0x55555555" => [ [ "0xFFFFFFFF", "0x8" ], ], "0xAAAAAAAA" => [ [ "0x55555554", "0x3" ], ], }, ); foreach my $item ( keys %temp ) { print "$item: \n", # print keys "\t", +( join "\t" => map { $_->[0], @{ $_->[1] }, "\n" } map { [ $_, @{ $temp{$item}->{$_} } ] } keys $tem +p{$item} ), $/; }

Re: How to access Hash of hashes with multi-dimensional Array
by hdb (Prior) on Apr 19, 2013 at 14:53 UTC

    I have attempted to create a simple, but annotated version of Data::Dumper. It prints the data structure and in the comment it tells you how to access this very element of the data structure on its own. I am not sure whether it is not confusing in a different way. If you don't mind looking at it and let me know whether it helps you...

    If this is your data

    my %temp = ( "0x55555555" => { "0x55555555" => [ ["0xAAAAAAAA", "0x9"] +, ], "0xAAAAAAAA" => [ ["0xAAAAAAAA", "0x8"], ], }, "0xAAAAAAAA" => { "0x55555555" => [ ["0xFFFFFFFF", "0x8"], ], "0xAAAAAAAA" => [ ["0x55555554", "0x3"], ], }, ); my $var = \%temp;

    it creates this output

    { # ref to hash: $var '0xAAAAAAAA' => # key of hash, string: +(keys %{$var})[0] { # ref to hash: $var->{'0xAAAAAAAA'} '0xAAAAAAAA' => # key of hash, string: +(keys %{$var->{'0xAA +AAAAAA'}})[0] [ # ref to array: $var->{'0xAAAAAAAA'}->{'0xAA +AAAAAA'} [ # ref to array: $var->{'0xAAAAAAAA'}->{'0xAA +AAAAAA'}->[0] '0x55555554', # scalar: $var->{'0xAAAAAAAA'}->{'0xAAAAAAAA +'}->[0]->[0] '0x3', # scalar: $var->{'0xAAAAAAAA'}->{'0xAAAAAAAA +'}->[0]->[1] ] ] '0x55555555' => # key of hash, string: +(keys %{$var->{'0xAA +AAAAAA'}})[1] [ # ref to array: $var->{'0xAAAAAAAA'}->{'0x55 +555555'} [ # ref to array: $var->{'0xAAAAAAAA'}->{'0x55 +555555'}->[0] '0xFFFFFFFF', # scalar: $var->{'0xAAAAAAAA'}->{'0x55555555 +'}->[0]->[0] '0x8', # scalar: $var->{'0xAAAAAAAA'}->{'0x55555555 +'}->[0]->[1] ] ] } '0x55555555' => # key of hash, string: +(keys %{$var})[1] { # ref to hash: $var->{'0x55555555'} '0xAAAAAAAA' => # key of hash, string: +(keys %{$var->{'0x55 +555555'}})[0] [ # ref to array: $var->{'0x55555555'}->{'0xAA +AAAAAA'} [ # ref to array: $var->{'0x55555555'}->{'0xAA +AAAAAA'}->[0] '0xAAAAAAAA', # scalar: $var->{'0x55555555'}->{'0xAAAAAAAA +'}->[0]->[0] '0x8', # scalar: $var->{'0x55555555'}->{'0xAAAAAAAA +'}->[0]->[1] ] ] '0x55555555' => # key of hash, string: +(keys %{$var->{'0x55 +555555'}})[1] [ # ref to array: $var->{'0x55555555'}->{'0x55 +555555'} [ # ref to array: $var->{'0x55555555'}->{'0x55 +555555'}->[0] '0xAAAAAAAA', # scalar: $var->{'0x55555555'}->{'0x55555555 +'}->[0]->[0] '0x9', # scalar: $var->{'0x55555555'}->{'0x55555555 +'}->[0]->[1] ] ] } }

    So if you pick any expression after the colon in the comment and use it or print it, it gives you exactly the part of the structure you pick it from. Couple of examples:

    print +(keys %{$var->{'0xAAAAAAAA'}})[1], "\n"; print $var->{'0x55555555'}->{'0xAAAAAAAA'}->[0]->[0], "\n"; print Dumper( $var->{'0x55555555'}->{'0x55555555'} ); gives you 0x55555555 0xAAAAAAAA $VAR1 = [ [ '0xAAAAAAAA', '0x9' ] ];

    Do I make sense?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2014-09-20 21:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (163 votes), past polls