Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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
Perl 需要与'的建议;如果';陈述_Perl_If Statement - Fatal编程技术网

Perl 需要与'的建议;如果';陈述

Perl 需要与'的建议;如果';陈述,perl,if-statement,Perl,If Statement,我需要帮助调整我的代码块。一切正常,但后来它停止工作,每次都失败(打印)。我做错了什么 print "Enter a word to search for:"; chomp (my $word = <STDIN> ); if (not -e $word){ print "No such word found.\n"; exit; } print“输入要搜索的单词:”; chomp(我的$word=); if(非-e$word){ 打印“找不到这样的词

我需要帮助调整我的代码块。一切正常,但后来它停止工作,每次都失败(打印)。我做错了什么

print "Enter a word to search for:";
chomp (my $word = <STDIN> );
if (not -e $word){
        print "No such word found.\n";
        exit;
}
print“输入要搜索的单词:”;
chomp(我的$word=);
if(非-e$word){
打印“找不到这样的词。\n”;
出口
}
整个计划

#!/usr/bin/perl -w


use strict;


print "Welcome to the word frequency calculator.\n";
print "This program prompts the user for a file to open, \n";
print "then it prompts for a word to search for in that file,\n";
print "finally the frequency of the word is displayed.\n";
print " \n";


print "Please enter the name of the file to search:";
chomp (my $filename = <STDIN> );
if (not -e $filename){
        print "No such file exists. Exiting program. Please try again.
+\n";
        exit;
}


print "Enter a word to search for:";
chomp (my $word = <STDIN> );
if (not -e $word){
        print "No such word found.\n";
        exit;
}


print "Frequency of word: " . grep $word eq $_,
split /\W+/i, do { local (@ARGV, $/)= $filename; <> };


exit;
#/usr/bin/perl-w
严格使用;
打印“欢迎使用单词频率计算器。\n”;
打印“此程序提示用户打开文件,\n”;
打印“然后提示输入要在该文件中搜索的单词,\n”;
打印“最后显示单词的频率。\n”;
打印“\n”;
打印“请输入要搜索的文件名:”;
chomp(我的$filename=);
if(非-e$filename){
打印“不存在此类文件。正在退出程序。请重试。”。
+\n”;
出口
}
打印“输入要搜索的单词:”;
chomp(我的$word=);
if(非-e$word){
打印“找不到这样的词。\n”;
出口
}
打印“单词频率:”。grep$word eq$\,
split/\W+/i,do{local(@ARGV,$/)=$filename;};
出口
因此,根据这一点,这个计划将

  • 向用户请求要搜索的文件
  • 请用户输入要搜索的单词
  • 检查该单词在该文件中的频率
  • 你把第一部分记下来了

    print "Please enter the name of the file to search:";
    chomp (my $filename = <STDIN> );
    if (not -e $filename){
            print "No such file exists. Exiting program. Please try again.\n";
            exit;
    }
    
    然后对于第二位,您只需要提示输入单词。检查这个词是否作为文件名存在是毫无意义的

    print "Enter a word to search for: ";
    chomp (my $word = <STDIN> );
    
    有关更多信息,请参阅

    因此,根据这一点,这个计划将

  • 向用户请求要搜索的文件
  • 请用户输入要搜索的单词
  • 检查该单词在该文件中的频率
  • 你把第一部分记下来了

    print "Please enter the name of the file to search:";
    chomp (my $filename = <STDIN> );
    if (not -e $filename){
            print "No such file exists. Exiting program. Please try again.\n";
            exit;
    }
    
    然后对于第二位,您只需要提示输入单词。检查这个词是否作为文件名存在是毫无意义的

    print "Enter a word to search for: ";
    chomp (my $word = <STDIN> );
    

    有关更多信息,请参阅。

    您希望该行做什么?因为它正在检查一个文件是否不存在,这在上下文中是没有意义的。而那种
    split
    是从文件中读取单词的一种非常奇怪的方式。。。如果行得通的话,你希望那条线做什么?因为它正在检查一个文件是否不存在,这在上下文中是没有意义的。而那种
    split
    是从文件中读取单词的一种非常奇怪的方式。。。如果行得通的话。除了你说的第二点,我什么都喜欢。你说得对,我不需要,但为了学习和变得复杂,我想去。(只是开玩笑)但是如果你有另一个建议涉及到我的“找不到这样的词”。声明将被感激。@distro你必须在搜索完这个词后再这样做。然后
    if($frequency==0){…}else{…}
    。除了你对第二位所说的以外,我什么都喜欢。你说得对,我不需要,但为了学习和变得复杂,我想去。(只是开玩笑)但是如果你有另一个建议涉及到我的“找不到这样的词”。声明将被感激。@distro你必须在搜索完这个词后再这样做。然后
    如果($frequency==0){…}否则{…}
    print "Frequency of word: " . grep $word eq $_,
      split /\W+/i, do { local (@ARGV, $/)= $filename; <> };
    
    my $frequency = 0;
    while( my $line = <$fh> ) {
        while( $line =~ /\Q$word\E/g ) {
            $frequency++
        }
    }