2

该脚本的目的是处理文件中的所有单词并输出出现次数最多的所有单词。因此,如果有 3 个单词每个出现 10 次,程序应该输出所有单词。

脚本现在可以运行了,这要归功于我在这里得到的一些提示。但是,它不处理大型文本文件(即新约)。我不确定这是我的错还是只是代码的限制。我确信该程序还有其他几个问题,因此将不胜感激任何帮助。

#!/usr/bin/perl -w
require 5.10.0;

print "Your file: " . $ARGV[0] . "\n";
#Make sure there is only one argument
if ($#ARGV == 0){

    #Make sure the argument is actually a file
    if (-f $ARGV[0]){

        %wordHash = ();     #New hash to match words with word counts
        $file=$ARGV[0];     #Stores value of argument
        open(FILE, $file) or die "File not opened correctly.";

        #Process through each line of the file
        while (<FILE>){
            chomp;
            #Delimits on any non-alphanumeric
            @words=split(/[^a-zA-Z0-9]/,$_);
            $wordSize = @words;

            #Put all words to lowercase, removes case sensitivty
            for($x=0; $x<$wordSize; $x++){
                $words[$x]=lc($words[$x]);
            }

            #Puts each occurence of word into hash
            foreach $word(@words){
                $wordHash{$word}++;
            }
        }
        close FILE;

        #$wordHash{$b} <=> $wordHash{$a};
        $wordList="";
        $max=0;

        while (($key, $value) = each(%wordHash)){
            if($value>$max){
                $max=$value;
            }
            }

        while (($key, $value) = each(%wordHash)){
            if($value==$max && $key ne "s"){
                $wordList.=" " . $key;
            }
            }       

        #Print solution
        print "The following words occur the most (" . $max . " times): " . $wordList . "\n";
    }
    else {
        print "Error. Your argument is not a file.\n";
    }
}
else {
    print "Error. Use exactly one argument.\n";
}
4

3 回答 3

6

您的问题在于脚本顶部缺少的两行:

use strict;
use warnings;

如果他们在那里,他们会报告很多这样的行:

Argument "make" isn't numeric in array element at ...

来自这一行:

$list[$_] = $wordHash{$_} for keys %wordHash;

数组元素只能是数字,并且由于您的键是单词,因此这是行不通的。这里发生的情况是任何随机字符串都被强制转换为一个数字,对于任何不以数字开头的字符串,这将是0.

您的代码可以很好地读取数据,尽管我会以不同的方式编写它。只有在那之后,您的代码才会变得笨拙。

据我所知,您正在尝试打印出出现次数最多的单词,在这种情况下,您应该考虑以下代码:

use strict;
use warnings;

my %wordHash;
#Make sure there is only one argument
die "Only one argument allowed." unless @ARGV == 1;
while (<>) {    # Use the diamond operator to implicitly open ARGV files
    chomp;
    my @words = grep $_,           # disallow empty strings
        map lc,                    # make everything lower case
            split /[^a-zA-Z0-9]/;  # your original split
    foreach my $word (@words) {
        $wordHash{$word}++;
    }
}

for my $word (sort { $wordHash{$b} <=> $wordHash{$a} } keys %wordHash) {
    printf "%-6s %s\n", $wordHash{$word}, $word;
}

正如您将注意到的,您可以根据哈希值进行排序。

于 2012-06-27T17:02:03.823 回答
1

这是一种完全不同的写法(我也可以说“Perl 不是 C”):

#!/usr/bin/env perl

use 5.010;
use strict; use warnings;
use autodie;

use List::Util qw(max);

my ($input_file) = @ARGV;
die "Need an input file\n" unless defined $input_file;

say "Input file = '$input_file'";

open my $input, '<', $input_file;

my %words;

while (my $line = <$input>) {
    chomp $line;

    my @tokens = map lc, grep length, split /[^A-Za-z0-9]+/, $line;
    $words{ $_ } += 1 for @tokens;
}

close $input;

my $max = max values %words;
my @argmax = sort grep { $words{$_} == $max } keys %words;

for my $word (@argmax) {
    printf "%s: %d\n", $word, $max;
}
于 2012-06-27T17:13:48.800 回答
0

为什么不直接从按值排序的哈希中获取键并提取第一个 X 呢?

这应该提供一个例子: http: //www.devdaily.com/perl/edu/qanda/plqa00016

于 2012-06-27T16:46:21.987 回答