# 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"; }