perl - Looping through known elements in a hash of hashes of arrays -


i have question hoping (simplified purposes of explaining question).

i have following hash of hashes of arrays (i think anyway?)

data structure

{   cat => {     height => ["tiny"],   },   dog => {     colour => ["black"],     height => ["tall"],     weight => ["fat", "huge"],   },   elephant => {     colour => ["grey"],     height => ["really big"],     weight => ["fat", "medium", "thin"],   }, } 

what trying do

the program below print whole data structure. i want use kind of way it

my %h;  $animal (keys %h) {    print "$animal\n";    $attribute ( keys %{$h{$animal}} ) {         print "\t $attribute\n";         $i (0 .. $#{$h{$animal}{$attribute}} ) {             print "\t\t$h{$animal}{$attribute}[$i]\n";         }        }    }           

the problem having

i trying access particular part of data structure. example, want print out height arrays each animal not care other colour, weight attributes in example.

i'm sure there simple answer this, , know need specify height part, correct way of doing it? have tried multiple ways thought work without success.

in code, instead of looping on all attributes with

for $attribute ( keys %{ $h{$animal} } ) { ... } 

just use 1 interested in. this

for $animal (keys %h) {    print "$animal\n";    $attribute ( 'height' ) {         print "\t $attribute\n";         $i (0 .. $#{$h{$animal}{$attribute}} ) {             print "\t$h{$animal}{$attribute}[$i]\n";         }        }    }           

i choose loop on contents of heights array rather indices, making code this:

for $animal (keys %h) {     print "$animal\n";     print "\t\t$_\n" @{ $h{$animal}{height} }; }           

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -