I am trying to print the line count, word count, character count, and print out the words in a file as well as the amount of times they occur. I am getting errors with the last part (i.e. printing the words out and their occurences). Everything else works fine.
The error message I get:
Bareword found where operator expected at wc.pl line 34, near ""Number of lines: $lcnt\","Frequency"
(Missing operator before Frequency?)
syntax error at wc.pl line 34, near ""Number of lines: $lcnt\","Frequency of "
Can't find string terminator '"' anywhere before EOF at wc.pl line 34.
Here is my code:
#!/usr/bin/perl -w
use warnings;
use strict;
my $lcnt = 0;
my $wcnt = 0;
my $ccnt = 0;
my %count;
my $word;
my $count;
open my $INFILE, '<', $ARGV[0] or die $!;
while( my $line = <$INFILE> ) {
$lcnt++;
$ccnt += length($line);
my @words = split(/\s+/, $line);
$wcnt += scalar(@words);
foreach $count(@words) {
$count{@words}++;
}
}
foreach $word (sort keys %count) {
print "Number of characters: $ccnt\n","Number of words: $wcnt\n","Number of lines: $lcnt\","Frequency of words in the file: $word : $count{$word}";
}
close $INFILE;
This is what I need it to do:
Sample input from txt file:
This is a test, another test
#test# 234test test234
Sample Output:
Number of characters: 52
Number of words: 9
Number of lines: 2
Frequency of words in the file:
--------------------------------
#test#: 1
234test: 1
This: 1
a: 1
another: 1
is: 1
test: 1
test,: 1
test234: 1
Any help would be greatly appreciated!