Actually, your step "2" is the only one that's necessary:
# for a simple case, where input is just a "key value" list, and
# when any key is repeated, we want to keep all its values in an array
+:
my %hash;
while (<>) {
my ( $k, $v ) = split;
push @{$hash{$k}}, $v;
}
for my $k ( keys %hash ) {
print "$k occurred ". scalar @{$hash{$k}} ." times\n";
}
(update: but it seems like the OP really wants a hash-of-hashes, as suggested in the first reply above, rather than a hash-of-arrays.) |