Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by atcroft (Monsignor)
on Sep 10, 2004 at 18:26 UTC ( #390153=categorized answer: print w/replies, xml ) Need Help??

Q&A > hashes > I've got a hash of hashes how do i get my values out - Answer 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

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 wandering the Monastery: (5)
As of 2016-10-01 22:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (9 votes). Check out past polls.