Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Perl脚本问题_Arrays_Perl_Hash_Initialization - Fatal编程技术网

Arrays Perl脚本问题

Arrays Perl脚本问题,arrays,perl,hash,initialization,Arrays,Perl,Hash,Initialization,脚本的目的是处理文件中的所有单词,并输出出现次数最多的所有单词。因此,如果有3个单词出现10次,程序应该输出所有单词 多亏了我在这里学到的一些技巧,脚本现在可以运行了。但是,它不处理大型文本文件(即新约)。我不确定这是我的错还是代码的限制。我确信这个程序还有其他一些问题,所以我们非常感谢您的帮助 #!/usr/bin/perl -w require 5.10.0; print "Your file: " . $ARGV[0] . "\n"; #Make sure there is only o

脚本的目的是处理文件中的所有单词,并输出出现次数最多的所有单词。因此,如果有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";
}
#/usr/bin/perl-w
要求5.10.0;
打印“您的文件:”$ARGV[0]。“\n”;
#确保只有一个参数
如果($#ARGV==0){
#确保参数实际上是一个文件
如果(-f$ARGV[0]){
%wordHash=();#将单词与单词计数进行匹配的新哈希
$file=$ARGV[0];#存储参数的值
打开(文件,$FILE)或“文件未正确打开”;
#处理文件的每一行
而(){
咀嚼;
#在任何非字母数字上定界
@文字=拆分(/[^a-zA-Z0-9]/,$);
$wordSize=@words;
#将所有单词改为小写,删除区分大小写
对于($x=0;最大$x$max){
$max=$value;
}
}
while(($key,$value)=每个(%wordHash)){
如果($value=$max&&$key ne“s”){
$wordList.=''.$key;
}
}       
#打印溶液
打印“以下单词出现次数最多(“.max.”次):“$wordList.”\n”;
}
否则{
打印“错误。您的参数不是文件。\n”;
}
}
否则{
打印“错误。只使用一个参数。\n”;
}

为什么不直接从散列中获取按值排序的键并提取第一个X


这应该提供一个示例:

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

use strict;
use warnings;
如果他们在那里,他们会报告很多这样的话:

参数“make”在…处的数组元素中不是数字。

这句话的意思是:

$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;
}
使用严格;
使用警告;
我的%wordHash;
#确保只有一个参数
die“只允许一个参数”。除非@ARGV==1;
while(){#使用菱形操作符隐式打开ARGV文件
咀嚼;
my@words=grep$#不允许使用空字符串
映射lc,#使所有内容都小写
拆分/[^a-zA-Z0-9]/#您原来的拆分
foreach我的$word(@words){
$wordHash{$word}++;
}
}
对于我的$word(排序{$wordHash{$b}$wordHash{$a}}关键字%wordHash){
printf“%-6s%s\n”,$wordHash{$word},$word;
}

正如您所注意到的,您可以根据散列值进行排序。

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

#/usr/bin/env perl
使用5.010;
严格使用;使用警告;
使用自动模具;
使用列表::Util qw(最大值);
我的($input_文件)=@ARGV;
除非定义了$input\U file,否则die“需要输入文件\n”;
说“输入文件='$Input_文件'”;
打开我的$input,'请在scriptsconsider中使用pragma“use strict;”)
#!/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;
}