Accessing Hash of hash & hash of array

#! /usr/bin/perl

package hash_of_hash;
use strict;
use Data::Dumper;

my %hash_of_hash = (
                     'a' => {'a1' => 'av1',
                             'a2' => 'av2',
                             'a3' => 'av3'
                            },
                     'b' => {'b1' => 'bv1',
                             'b2' => 'bv2',
                             'b3' => 'bv3'

                            }
                   );

my %hash_of_ary = (
                     'a' => ['a1','av1','a2','av2','a3','av3'],
                     'b' => ['b1','bv1','b2','bv2','b3','bv3']
                   );           


print_hash_of_hash(\%hash_of_hash);
print_hash_of_ary(\%hash_of_ary);

sub print_hash_of_hash
{
  my $hash_of_hash_ref = shift;
  my %hash_of_hash = %$hash_of_hash_ref;
     foreach my $hash_key (sort keys %hash_of_hash)
     {
         print  "hash_of_hash Key $hash_key:\n";
         foreach my $hash_of_hash_key(sort keys %{$hash_of_hash{$hash_key}}) # Note % is imp
              {
               print "       Hash of Hash is    $hash_of_hash_key ==> $hash_of_hash{$hash_key}{$hash_of_hash_key}\n";
              }
    }
}


sub print_hash_of_ary
{
  my $hash_of_ary_ref = shift;
  my %hash_of_ary = %$hash_of_ary_ref;
     foreach my $hash_key (sort keys %hash_of_hash)
     {
         print  "hash_of_ary Key $hash_key:\n";
         foreach my $hash_of_ary (@{$hash_of_ary{$hash_key}}) # Note @ is imp
              {
               print "         Hash of ary is $hash_of_ary\n";
              }
    }
}

========= OUT PUT ===========
hash_of_hash Key a:
       Hash of Hash is    a1 ==> av1
       Hash of Hash is    a2 ==> av2
       Hash of Hash is    a3 ==> av3
hash_of_hash Key b:
       Hash of Hash is    b1 ==> bv1
       Hash of Hash is    b2 ==> bv2
       Hash of Hash is    b3 ==> bv3
hash_of_ary Key a:
         Hash of ary is a1
         Hash of ary is av1
         Hash of ary is a2
         Hash of ary is av2
         Hash of ary is a3
         Hash of ary is av3
hash_of_ary Key b:
         Hash of ary is b1
         Hash of ary is bv1
         Hash of ary is b2
         Hash of ary is bv2
         Hash of ary is b3
         Hash of ary is bv3

No comments: