Hash

%HoH = (
        flintstones => {
                lead      => "fred",
                pal       => "barney",
              Mount1 => {
                             'A'  => '1',
                          },


              main  => "Common",

        },

        mainoutside => "Commonoutside",    # like to display this part of Hash

 );

 foreach $family ( keys %HoH ) {

     print "$family: { ";
     for $role( keys %{ $HoH{$family} } ) {
         if( ref $HoH{$family}{$role} eq 'HASH' ){
         print "$role: { ";
           for $role1 ( keys %{ $HoH{$family}{$role} } ) {
             print "$role1=$HoH{$family}{$role}{$role1} ";
           }
         print "} ";
        }
        elsif (ref $HoH{$family} eq 'SCALAR')   # Added this code to catch that value
        {

        print "\n $family=$HoH{$family}\n";

        }
        else{
           print "$role=$HoH{$family}{$role} ";
        }
   }
      print "}\n";
   }

==============================================
output ::
mainoutside: { }  # Wrong output
flintstones: { lead=fred Mount1: { A=1 } pal=barney main=Common }

Question :: How to read ""  mainoutside => "Commonoutside",     "" of hash ?


gauravflameAsked:
Who is Participating?
 
ozoCommented:
foreach $family ( keys %HoH ) {

     print "$family: { ";
     if( ref $HoH{$family} eq "HASH" ){
       for $role( keys %{ $HoH{$family} } ) {
         if( ref $HoH{$family}{$role} eq 'HASH' ){
         print "$role: { ";
           for $role1 ( keys %{ $HoH{$family}{$role} } ) {
             print "$role1=$HoH{$family}{$role}{$role1} ";
           }
         print "} ";
        }
        elsif (ref $HoH{$family} eq 'SCALAR')   # Added this code to catch that value
        {

        print "\n $family=$HoH{$family}\n";

        }
        else{
           print "$role=$HoH{$family}{$role} ";
        }
     }
   }else{
         print $HoH{$family};
   }


      print "}\n";
   }
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.