Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

I've got a hash of hashes how do i get my values out

( #116162=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie progressive on Oct 02, 2001 at 19:57 UTC
Q&A  > hashes


Description:

eg.
my %hash = (key1 => {key1a => 'value', key1b => 'value',} };
I now want to use a foreach loop to get the values out. How is this done??

Answer: I've got a hash of hashes how do i get my values out
contributed by C-Keen

Simple as stated in the Camel:

foreach $item (keys %hash){ print "$item: "; foreach $iteminitem (keys %{$hash{$item}}){ print "$iteminitem = $hash{$item}{$iteminitem} "; } print "\n"; }

Read the chapter on Data structures in the Camel book.

Happy coding,
C-Keen

Answer: I've got a hash of hashes how do i get my values out
contributed by atcroft

Perhaps not the most elegant of ways to handle this (I suspect it can be done better), and may go a little further than you intended (I am not sure this way would necessarily be recommendable), but perhaps it may prove useful. The code can handle a (multilevel structure that consists only of scalars, arrays, and hashes, which it sends to a function for processing (which, in this case, is only display). Hope it helps.

Code:

#!/usr/bin/perl -w use strict; my %hash = ( key1 => { key2 => { key3 => 'asdf', key3b => ';lkj', key3c => { key4 => 'a', key4b => [ 0 .. 3 ], key4c => 'c' } }, key2b => 'wer' } ); my @array = ( 0 .. 5, [ 100 .. 105, [ 1000 .. 1005 ], 107 .. 109 ], 7 .. 9 ); my $scalar = 'blah'; dump_data( 0, '$hash', \%hash ); dump_data( 0, '$array', \@array ); dump_data( 0, '$scalar', \$scalar ); sub dump_data { my ( $level, $base, $data ) = @_; my $nextlevel = $level + 1; if ( ref($data) eq 'ARRAY' ) { foreach my $k ( 0 .. $#{$data} ) { my $baseval = $base . '[' . $k . ']'; dump_it( $nextlevel, $baseval, $data->[$k] ); } } elsif ( ref($data) eq 'HASH' ) { foreach my $k ( sort( keys( %{$data} ) ) ) { my $baseval = $base . '{' . $k . '}'; dump_it( $nextlevel, $baseval, $data->{$k} ); } } elsif ( ref($data) eq 'SCALAR' ) { my $baseval = $base; dump_it( $nextlevel, $baseval, ${$data} ); } } sub dump_it { my ( $nextlevel, $baseval, $datum ) = @_; my $reftype = ref($datum); if ( $reftype eq 'HASH' ) { dump_data( $nextlevel, $baseval, \%{$datum} ); } elsif ( $reftype eq 'ARRAY' ) { dump_data( $nextlevel, $baseval, \@{$datum} ); } else { process_data( $nextlevel, $baseval, $datum ); } } sub process_data { my ( $nextlevel, $baseval, $datum ) = @_; my $indentation = ' ' x $nextlevel; print $indentation, $baseval, ' = ', $datum, "\n"; }

Sample run results:

$ perl test-mlh.pl $hash{key1}{key2}{key3} = asdf $hash{key1}{key2}{key3b} = ;lkj $hash{key1}{key2}{key3c}{key4} = a $hash{key1}{key2}{key3c}{key4b}[0] = 0 $hash{key1}{key2}{key3c}{key4b}[1] = 1 $hash{key1}{key2}{key3c}{key4b}[2] = 2 $hash{key1}{key2}{key3c}{key4b}[3] = 3 $hash{key1}{key2}{key3c}{key4c} = c $hash{key1}{key2b} = wer $array[0] = 0 $array[1] = 1 $array[2] = 2 $array[3] = 3 $array[4] = 4 $array[5] = 5 $array[6][0] = 100 $array[6][1] = 101 $array[6][2] = 102 $array[6][3] = 103 $array[6][4] = 104 $array[6][5] = 105 $array[6][6][0] = 1000 $array[6][6][1] = 1001 $array[6][6][2] = 1002 $array[6][6][3] = 1003 $array[6][6][4] = 1004 $array[6][6][5] = 1005 $array[6][7] = 107 $array[6][8] = 108 $array[6][9] = 109 $array[7] = 7 $array[8] = 8 $array[9] = 9 $scalar = blah
Answer: I've got a hash of hashes how do i get my values out
contributed by hardburn

For HoHes nested arbitrarily deep:

sub walk_hash { my $h = shift; foreach my $key (keys %$h) { if( ref $h->{$key}) { walk_hash( $h->{$key} ); } else { print $h->{$key}; } } }

This has been tested.

Answer: I've got a hash of hashes how do i get my values out
contributed by davorg

Or, to go directly to a particular value given the two keys that define it:

my $val = $hash{$key1}{$key2};
Answer: I've got a hash of hashes how do i get my values out
contributed by cgall

for debugging purposes, you may find Data::Dumper useful:

use Data::Dumper; print Dumper(%hash);

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (15)
    As of 2015-07-31 09:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (276 votes), past polls