我正在嘗試打印行數,字數,字符數,並打印出文件中的單詞以及它們出現的次數。我在最後一部分出現錯誤(即將單詞輸出並出現)。其他一切正常。在文本文件中打印單詞頻率Perl
錯誤消息我得到:
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.
這裏是我的代碼:
#!/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;
這是我需要做的:從txt文件
樣品輸入:
This is a test, another test
#test# 234test test234
樣本輸出:
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
任何幫助將不勝感激!
我很感激幫助! @DavidO – chomp 2014-11-06 05:34:49