in reply to counting number of occurrences of words in a file

"The" ne "the". You should normalise the case.

Other issues:

use strict; use warnings; my $file; { my $qfn = '<insertfilepath>'; open(my $FILE, '<', $qfn) or die("Can't open \"$qfn\": $!\n"); local $/; $file = <$FILE>; } my %word_counts; for (split(' ', $file)) { s/[,.!?:;"'<>]//g; ++$word_counts{lc($_)}; } for my $word (sort keys(%word_counts)) { print "$word occurred $word_counts{$word} times\n"; }

Update: There's no reason to load the entire file into memory at once, and if you don't, you gain the ability to pass a file name on the command line.

use strict; use warnings; my %word_counts; while (<>) { for (split(' ', $_)) { s/[,.!?:;"'<>]//g; ++$word_counts{lc($_)}; } } for my $word (sort keys(%word_counts)) { print "$word occurred $word_counts{$word} times\n"; }