The following demo builds a counter (using a hash of hashes) into
memory from the DATA section and then displays the result. However,
I'm not terribly fond of the way I create the hashes in the hash.
This demo of course is a simplification of my actual use case,
where I ended up having three-layered hashes embedded with
complex initialization.
Does anyone have suggestions for a "neater" way to do this?
----
#!/usr/bin/perl
use strict;
use warnings;
# Collect DATA into hash of hashes
my %data;
while(<DATA>) {
chomp;
(my $a, my $b) = split(/\s+/,$_,2);
if(defined $data{$a}) {
$data{$a}{$b}++;
} else {
$data{$a} = { $b => 1 };
}
}
# Print out collected data
foreach my $a (sort keys %data) {
print $a;
foreach my $b (sort keys $data{$a}) {
print "\t",$b,"\t",$data{$a}{$b},"\n";
}
}
__DATA__
yy cc
xx aa
yy aa
yy bb
xx cc
zz cc
yy bb
xx bb
yy aa
--
/* * * Otto J. Makela <
om@iki.fi> * * * * * * * * * */
/* Phone: +358 40 765 5772, ICBM: N 60 10' E 24 55' */
/* Mail: Mechelininkatu 26 B 27, FI-00100 Helsinki */
/* * * Computers Rule 01001111 01001011 * * * * * * */
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)