【发布时间】:2015-01-02 12:27:34
【问题描述】:
我正在尝试打印行数、字数、字符数,并打印出文件中的单词以及它们出现的次数。最后一部分出现错误(即打印出单词及其出现)。其他一切正常。
我得到的错误信息:
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
任何帮助将不胜感激!
【问题讨论】:
标签: perl scripting scripting-language