use strict; my %hash; while(){ chomp; last if $_ =~ /^q$/i; # quit if 'q' is entered if(exists $hash{$_}){ # exists, so increment $hash{$_}++; } else{ # doesn't exist, so set to 1 $hash{$_} = 1; } } my @array = sort keys %hash; # let's make the output sorted... foreach(@array){ print "$_ : $hash{$_}\n"; }