Fellow monks,
I seek enlightenment on how to generate a hash of hashes with constant keys. I have a list of countries in an array like so:
@countries = (Algeria, Argentina...) which will form the keys of the inner hashes, and a file containing data in the following format:
Q4_2001: 741 1203 3406 1861 1242 1911 601 etc
Q3_2001: 773 1255 3552 1941 1296 1993 627 etc
Q2_2001: 805 1307 3698 2021 1350 2075 653 etc
I would like to build this into a hash of hashes thus:
%HoH = (
Q4_2001 => {
Algeria => 741,
Argentina => 1203, etc
},
Q3_2001 => {
Algeria => 773,
Argentina => 1255, etc
},
);
However, I am fairly new to Perl, and my attempts to write a sub to generate this HoH have not met with success, even after much perusal of perldsc, perllol, etc. Here's what I have so far:
sub build_HoH {
while (<DATA>) {
next unless s/^(.*?):\s*//;
my $period = $1;
my @data = split;
foreach my $country(@countries) {
$HoH{$period}{$country} = $data;
}
}
I would be grateful for any brotherly advice as to where I am going wrong. Humble thanks in advance.