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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.