Perl Hash of Hashes creation

160 views Asked by At

I have a log file which looks like as follows.

Year:2001
State: A
District A  
District B
State: B
District A
District B
Year:2002
State: A
District A
District B
State: B
District A
District B
.
.
Year:2012
State: A
District A  
District B
State: B
District A
District B

I would like to have a hash of hash such that:

$VAR2 = {'2001' => {
                 'state A' => { district a
                                district b
                              } 
                  'state B' => { district a
                                 district b
                              }                   
                  }
         2002' => {
                 'state A' => { district a
                                district b
                              } 
                  'state B' => { district a
                                 district b
                              }                   
                  }
};

I have tried the above logic using 3 nested loops as follows:

foreach my $key (keys %hash) {
    foreach my $key2 (keys %{ $hash{$key} }) {
        foreach my $key3 (keys %{ $hash{$key}{$key2} }) {
        $value = $hash{$key}{$key2}->{$key3};
        }
    }
}

Please can somebody explain me the procedure to do it. Or atleast inform me if I am going in the write path. Thank you.

1

There are 1 answers

2
Toto On BEST ANSWER

You could do something like:

use strict;
use warnings;
use 5.010;
use Data::Dumper;

my $hash;
my $year;
my $state;
while(<DATA>) {
    chomp;
    if(/^Year:(\d+)/) {
        $year = $1;
        $hash->{$year} = {};
        next;
    } elsif (/^State:/) {
        $state = $_;
        $hash->{$year}{$state} = [];
        next;
    } elsif(/^District/) {
        push @{$hash->{$year}{$state}}, $_;
    }
}
say Dumper$hash;


__DATA__
Year:2001
State: A
District A  
District B
State: B
District A
District B
Year:2002
State: A
District A
District B
State: B
District A
District B
Year:2012
State: A
District A  
District B
State: B
District A
District B

output:

$VAR1 = {
          '2002' => {
                    'State: B' => [
                                  'District A',
                                  'District B'
                                ],
                    'State: A' => [
                                  'District A',
                                  'District B'
                                ]
                  },
          '2001' => {
                    'State: B' => [
                                  'District A',
                                  'District B'
                                ],
                    'State: A' => [
                                  'District A  ',
                                  'District B'
                                ]
                  },
          '2012' => {
                    'State: B' => [
                                  'District A',
                                  'District B'
                                ],
                    'State: A' => [
                                  'District A  ',
                                  'District B'
                                ]
                  }
        };