perl - Get value from hash of hashes -


i value hash of hashes not. code :

sub test {     $filename = $_[0];     open infile, ${filename} or die $!;     %hashcount;     @firstline = split('\t',<infile>);     shift(@firstline);     while (my $line = <infile>)      {         %temp;         chomp($line);         @line = split('\t', $line);         foreach $cpt (1..$#line) {             $temp{$firstline[$cpt-1]}=$line[$cpt];         }         $hashcount{$line[0]}={%temp};     }     return %hashcount; } sub get_hash_of_hash {      $h = shift;      foreach $key (keys %$h) {          if( ref $h->{$key}) {              get_hash_of_hash( $h->{$key} );         }          else {              $h->{$key};         }      }  } 

and when display hash :

$var10679 = 'm00967_43_1106_2493_14707'; $var10680 = {           'a' => '1',           'b' => '0',           'c' => '1',           'd' => '0',           'e' => '0'         }; 

my first function return hash of hashes , specific value second function. want value :

my %hashtest = test("file.txt"); get_hash_of_hash(%hashtest,"m00967_43_1106_2493_14707","a") //return value '1'

you can either access nested elements like

$hash{keya}{keyb} 

or can write function walks data structure, like

sub walk {   ($hashref, @keys) = @_;   $pointer = $hashref;   $key (@keys) {     if (exists $pointer->{$key}) {       $pointer = $pointer->{$key};     } else {       die "no value @ ", join "->", @keys;     }   }   return $pointer; } 

which can used like

my %hash = (   'm00967_43_1106_2493_14707' => {       'a' => '1',       'b' => '0',       'c' => '1',       'd' => '0',       'e' => '0'   }, ); walk(\%hash, 'm00967_43_1106_2493_14707', 'a'); 

note: when using data::dumper, pass references dump function:

print dump \%hash; # not print dump %hash 

this neccessary show correct data structure.


Comments