Home > database >  Creating sub-hashes in main hash
Creating sub-hashes in main hash

Time:08-11

I want to create a hash of hashes in Perl without having to explicitly write everything out. I understand that I could use dclone to do something like:

use Storable 'dclone';

my %main_hash = (
   A => {},
   B => {},
   C => {},
);

my %sub_hash = (
   a => [],
   b => [],
   c => [],
   d => [],
);

foreach my $main_key (keys %main_hash) {
   $main_hash{$main_key} = dclone {%sub_hash};
}

Final result:

%main_hash:
   A => {
     a => [],
     b => [],
     c => [],
     d => [],
   },
   B => {
     a => [],
     b => [],
     c => [],
     d => [],
   },
   C => {
     a => [],
     b => [],
     c => [],
     d => [],
   },
);

Is there any way to do this repeated hash insertion without relying on dclone or some other imported module?

CodePudding user response:

You can just put the %sub_hash declaration inside the loop and assign it to the main hash. Each loop iteration will be a new hash, you don't need dclone:

my %main_hash = (
   A => {},
   B => {},
   C => {},
);

foreach my $main_key (keys %main_hash) {
    my %sub_hash = (
       a => [],
       b => [],
       c => [],
       d => [],
    );
   $main_hash{$main_key} = \%sub_hash;
}

use Data::Dumper;
print Dumper \%main_hash;

Output:

$VAR1 = {
          'C' => {
                   'b' => [],
                   'a' => [],
                   'c' => [],
                   'd' => []
                 },
          'B' => {
                   'b' => [],
                   'a' => [],
                   'c' => [],
                   'd' => []
                 },
          'A' => {
                   'c' => [],
                   'd' => [],
                   'b' => [],
                   'a' => []
                 }
        };

CodePudding user response:

Please inspect following code snippet which utilizes two indexes to generate a hash -- for compliance with your problem.

use strict;
use warnings;
use feature 'say';

use Data::Dumper;

my %hash;

for my $i ( qw/A B C/ ) {
    for my $j ( qw/a b c d/ ) {
        $hash{$i}{$j} = [];
    }
}

say Dumper(\%hash);

Output

$VAR1 = {
          'C' => {
                   'c' => [],
                   'd' => [],
                   'a' => [],
                   'b' => []
                 },
          'B' => {
                   'b' => [],
                   'a' => [],
                   'c' => [],
                   'd' => []
                 },
          'A' => {
                   'c' => [],
                   'd' => [],
                   'b' => [],
                   'a' => []
                 }
        };
  • Related