in reply to counting number of occurrences of words in a file
"The" ne "the". You should normalise the case.
Other issues:
You use use warnings;. Great! You should also use use strict;, though.
Why loop when you know you're only going to get one value?
should beundef($/); while (<FILE>) { }
undef($/); $_ = <FILE>;
Use of alternation inside character class.
is the same ass/[\,|\.|\!|\?|\:|\;|\"|\'|\<|\>]//g;
ands/[\,\.\!\?\:\;\"\'\<\>||||||||]//g;
You want an alternations/[\,\.\!\?\:\;\"\'\<\>|]//g;
or a character classs/\,|\.|\!|\?|\:|\;|\"|\'|\<|\>//g;
s/[\,\.\!\?\:\;\"\'\<\>]//g;
Useless escaping. For readability,
should be@array = split(/\ /, $_); s/[\,\.\!\?\:\;\"\'\<\>]//g;
@array = split(/ /, $_); s/[,.!?:;"'<>]//g;
is the same as just'<insertfilepath>' || $!
'<insertfilepath>'
since the file name will always be a true value.
Useless use of global variables (FILE), and unlocalised changes to global variables ($/).
Splitting on spaces won't split on newlines, and will produce empty strings when there are two spaces in a row. Split on special ' ' instead.
should be@array = split(/ /, $_);
@array = split(' ', $_);
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"; }
|
|---|