Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by progressive (Initiate)
on Oct 02, 2001 at 19:57 UTC ( [id://116162]=perlquestion: print w/replies, xml ) Need Help??

progressive has asked for the wisdom of the Perl Monks concerning the following question:

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??

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: I've got a hash of hashes how do i get my values out
by atcroft (Abbot) on Sep 10, 2004 at 18:26 UTC

    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:

Re: I've got a hash of hashes how do i get my values out
by C-Keen (Monk) on Oct 02, 2001 at 20:04 UTC
    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

Re: I've got a hash of hashes how do i get my values out
by hardburn (Abbot) on Sep 10, 2004 at 16:11 UTC

    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.

Re: I've got a hash of hashes how do i get my values out
by cgall (Acolyte) on Oct 13, 2001 at 01:23 UTC
Re: I've got a hash of hashes how do i get my values out
by davorg (Chancellor) on Oct 03, 2001 at 12:44 UTC

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

    my $val = $hash{$key1}{$key2};

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-03-19 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found