my %count; my $size = 4; # minimum size of words to count while (<>) { my @words = split; foreach my $word (@words) { next if length($word) < $size; # Count only "real" words, and words with # hyphens and apostrophes, like /foo-bar/ # and /don't/ and /ma'am/ $count{$word}++ if $word =~ m/^[a-z][A-Z]+([-'][a-z][A-Z]+)*$/; } } my %uniq = keys %count; unless (%uniq) { die "No words in the file\n"; } foreach my $word (keys %count) { $final_count += $count{$word}; $final_length += length($word); } # Do some other stuff with your hash