Tuesday 31 January 2023

Perl Datastructures - Hash of Hash

Perl hash of hashes is a data structure that consists of a hash that contains other hashes as values. Each hash contained in the main hash can be accessed using a unique key.

Here's an example of how to create a hash of hashes in Perl:

my %HoH = (
   'John' => {
      'age' => 35,
      'occupation' => 'Programmer',
      'salary' => 50000,
   },
   'Mary' => {
      'age' => 27,
      'occupation' => 'Designer',
      'salary' => 40000,
   },
   'Mark' => {
      'age' => 42,
      'occupation' => 'Manager',
      'salary' => 70000,
   },
);


In above example, %HoH is a hash of hashes that contains three keys: John, Mary, and Mark. Each key maps to another hash that contains three keys: age, occupation, and salary.

To access the values stored in the hash of hashes, you can use the following syntax:

print $HoH{'John'}{'occupation'}; # Output: Programmer
print $HoH{'Mary'}{'salary'};     # Output: 40000
print $HoH{'Mark'}{'age'}; 


In above example, we are accessing the values stored in the inner hashes by specifying the keys of the outer hash and the keys of the inner hash separated by the arrow operator ->.

To iterate over a hash of hashes in Perl, you can use nested loops to access each key-value pair in the inner hashes. Here's an example:

my %HoH = (
   'John' => {
      'age' => 35,
      'occupation' => 'Programmer',
      'salary' => 50000,
   },
   'Mary' => {
      'age' => 27,
      'occupation' => 'Designer',
      'salary' => 40000,
   },
   'Mark' => {
      'age' => 42,
      'occupation' => 'Manager',
      'salary' => 70000,
   },
);

foreach my $outer_key (keys %HoH) {
   print "Name: $outer_key\n";
   foreach my $inner_key (keys %{ $HoH{$outer_key} }) {
      my $value = $HoH{$outer_key}{$inner_key};
      print "  $inner_key: $value\n";
   }
}


In above example, we use two nested loops to iterate over the hash of hashes. The outer loop iterates over the keys of the main hash, which gives us access to the inner hashes. The inner loop iterates over the keys of each inner hash using the %{ $HoH{$outer_key} } syntax.

Inside the inner loop, we access the value of each key-value pair using the $HoH{$outer_key}{$inner_key} syntax. We then print out the name of the person, followed by the key and value of each attribute.

This will output:

Name: John
  age: 35
  occupation: Programmer
  salary: 50000
Name: Mary
  age: 27
  occupation: Designer
  salary: 40000
Name: Mark
  age: 42
  occupation: Manager
  salary: 70000


Create Hash Hash in Multiple Methods:

Method 1:

%HoH = (
    flintstones => {
        husband   => "fred",
        pal       => "barney",
    },
    jetsons => {
        husband   => "george",
        wife      => "jane",
        "his boy" => "elroy",  # Key quotes needed.
    },
    simpsons => {
        husband   => "homer",
        wife      => "marge",
        kid       => "bart",
    },
);


foreach (keys %HoH)
{
    %Hash=%{$HoH{$_}};
    foreach $key (keys %Hash)
    {
        print "$key => $Hash{$key}\n";
    }
}


Method 2:

$HoH = {
    flintstones => {
        husband   => "fred",
        pal       => "barney",
    },
    jetsons => {
        husband   => "george",
        wife      => "jane",
        "his boy" => "elroy",  # Key quotes needed.
    },
    simpsons => {
        husband   => "homer",
        wife      => "marge",
        kid       => "bart",
    },
};



foreach (keys %{$HoH})
{
    %Hash=%{$HoH->{$_}};
    foreach $key (keys %Hash)
    {
        print "$key => $Hash{$key}\n";
    }
}


Using Loops:  

To create a hash of hashes in Perl using a loop, you can start with an empty hash and then add keys and values to it using a loop. Here's an example:

my %HoH = ();

for my $outer_key ('outer_key_1', 'outer_key_2', 'outer_key_3') {
   for my $inner_key (1..3) {
      my $value = "value_" . ($inner_key + 3 * ($outer_key =~ /(\d+)/)[0]);
      $HoH{$outer_key}{"inner_key_$inner_key"} = $value;
   }
}


In above example, we used two nested loops to create a hash of hashes. The outer loop iterates over the outer keys (outer_key_1, outer_key_2, and outer_key_3), while the inner loop iterates over the inner keys (inner_key_1, inner_key_2, and inner_key_3).

Inside the inner loop, we generate the value for each key-value pair using some logic. In this case, we concatenate the string "value_" with the value of $inner_key plus three times the number that we extract from the outer key using a regular expression.

Finally, we add the key-value pair to the hash of hashes using the syntax $HoH{$outer_key}{"inner_key_$inner_key"} = $value.

This will create a hash of hashes %HoH that contains three outer keys outer_key_1, outer_key_2, and outer_key_3. Each outer key maps to another hash that contains three inner keys inner_key_1, inner_key_2, and inner_key_3.

To access the values stored in the hash of hashes, you can use the following syntax:

print $HoH{'outer_key_1'}{'inner_key_1'}; # Output: value_1
print $HoH{'outer_key_2'}{'inner_key_2'}; # Output: value_6
print $HoH{'outer_key_3'}{'inner_key_3'}; # O


Using Push Method:

To create a Perl hash of hash using the push method, you first need to create an empty hash, and then add key-value pairs to it using the push method. Here is an example code snippet:

my %hash_of_hash;

# Add a key-value pair to the hash of hash

push @{$hash_of_hash{key1}}, { subkey1 => 'value1', subkey2 => 'value2' };

# Add another key-value pair to the hash of hash

push @{$hash_of_hash{key2}}, { subkey3 => 'value3', subkey4 => 'value4' };


In above example, %hash_of_hash is an empty hash of hash. The push method is used to add a new key-value pair to the hash of hash. The first argument to the push method is a reference to an array that is associated with the key in the outer hash. The second argument is a reference to a hash that represents the inner hash.

the push method adds two key-value pairs to %hash_of_hash. The first key-value pair has a key of key1 and a value that is a reference to an array containing a single hash. The hash has two key-value pairs: subkey1 with a value of 'value1', and subkey2 with a value of 'value2'. The second key-value pair has a key of key2 and a value that is a reference to an array containing a single hash. The hash has two key-value pairs: subkey3 with a value of 'value3', and subkey4 with a value of 'value4'.


kaavannan blogspot

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home