#!usr/bin/perl -w use strict; my %date_hash; my @data = qw( NCLEGEND-11-20 NCLEGEND-11-20 NCLEGEND-11-2 NCLEGEND-1-10 NCLEGEND-1-10 NCLEGEND-1-10 NCLEGEND-1-20 NCLEGEND-1-20); foreach my $line (@data) { chomp ($line); #needed if @data is a file handle $line =~ s/-(\d)-/-0$1-/; #add leading zero for month $line =~ s/-(\d)$/-0$1/; #add leading zero for date my $date = ($line =~ m/NCLEGEND\-([\d-]+)/)[0]; #get the "num" part $date_hash{$date}++; } foreach my $date (sort keys %date_hash) { print "$date $date_hash{$date}\n"; #just print "$date\n"; if no need for counter value } __END__ Prints: 01-10 3 01-20 2 11-02 1 11-20 2