use strict; use warnings; my %matches = (); while () { if (/Good\s(\d)/) { $matches{$1}++; } } foreach my $key (keys %matches) { print "$key: $matches{$key}\n"; } __DATA__ Good 1 Good 2 Good 3 Bad 1 Bad 2 Bad 1 Good 3 Good 1