chomp
chomp

Reputation: 125

Print word frequencies in a text file Perl

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!

Upvotes: 2

Views: 1144

Answers (2)

DavidO
DavidO

Reputation: 13942

There are some logic errors, and some variable misuses in your code. For logic errors, you really only need to print "Number of characters" once, but you put it in a loop, along with a few others that should be printed only once. Pull them out of the loop.

Next, you weren't counting correctly; you were never actually using the word in your foreach $count (@words) line. That's what I called a variable misuse; "$count{@words}++" is definitely not what you wanted.

There was one typo as well, which was causing Perl to issue a syntax error. That was the missing n from \n. An easy fix.

Finally, we'll try to do a better job of declaring variables in the narrowest scope possible. Here's how it could look:

my $lcnt = 0;
my $wcnt = 0;
my $ccnt = 0;
my %count;

while( my $line = <DATA> ) {

    $lcnt++;
    $ccnt += length($line);

    my @words = split(/\s+/, $line);
    $wcnt += scalar(@words);

    foreach my $word (@words) {
        $count{$word}++;
    }
}

print "Number of characters: $ccnt\n",
      "Number of words: $wcnt\n",
      "Number of lines: $lcnt\n",
      "Frequency of words in the file:\n",
      "-----------------------------------\n";

foreach my $word (sort keys %count) {
    print "$word: $count{$word}\n";
}

__DATA__
This is a test, another test
#test# 234test test234

I switched over to using the __DATA__ filehandle for now just for simplicity. You can easily switch back to opening an input file.

Upvotes: 2

harvey
harvey

Reputation: 2953

It looks like you meant to do a \n but instead did a \" which escapes the end of string quote.

Change from;

... "Number of lines: $lcnt\","Frequency of ...

To;

... "Number of lines: $lcnt\n","Frequency of ...

Upvotes: 1

Related Questions