#this is assuming you are in the right directory #this is not the most efficient way of getting a list of files foreach (<*.*>) { my($month,$year)=split(/\d{2}/,$_); $year+=1900; #Assuming that year started after 1990 if($year<90){$year+=100;} $NEWSLETTER{$year}{$month} = 0; } foreach $y (sort keys %NEWSLETTER) { foreach $m (sort keys $NEWSLETTER{$y}) { #insert code for what you want to do with the sorted year and month.... } }