【问题标题】:Print word frequencies in a text file Perl在文本文件 Perl 中打印词频
【发布时间】: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


    【解决方案1】:

    看起来你打算做一个 \n 而是做了一个 \" ,它转义了字符串引号的结尾。

    变化自;

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

    到;

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

    【讨论】:

      【解决方案2】:

      您的代码中存在一些逻辑错误和变量误用。对于逻辑错误,你真的只需要打印一次“字符数”,但是你把它和其他一些应该只打印一次的人一起放在一个循环中。将它们拉出循环。

      接下来,您的计数不正确;您实际上从未在foreach $count (@words) 行中使用过这个词。这就是我所说的变量滥用; “$count{@words}++”绝对不是你想要的。

      还有一个错字,导致 Perl 发出语法错误。那是\n 中缺少的n。一个简单的修复。

      最后,我们将尝试在尽可能窄的范围内更好地声明变量。下面是它的外观:

      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
      

      为了简单起见,我现在改用__DATA__ 文件句柄。您可以轻松切换回打开输入文件。

      【讨论】:

      • 感谢您的帮助! @DavidO
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 2022-11-26
      • 1970-01-01
      • 2012-01-27
      • 2021-01-31
      相关资源
      最近更新 更多