2014-11-06 73 views
2

我正在嘗試打印行數,字數,字符數,並打印出文件中的單詞以及它們出現的次數。我在最後一部分出現錯誤(即將單詞輸出並出現)。其他一切正常。在文本文件中打印單詞頻率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 

任何幫助將不勝感激!

回答

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__文件句柄現在只是爲了簡單起見。您可以輕鬆切換回打開輸入文件。

+1

我很感激幫助! @DavidO – chomp 2014-11-06 05:34:49

1

它看起來像你的意思做一個\ n,而是做了\」的轉義字符串引號結束

從;更改。

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

要;

... "Number of lines: $lcnt\n","Frequency of ... 
相關問題