Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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 - Fatal编程技术网

Perl:搜索文件

Perl:搜索文件,perl,Perl,我正在创建一个perl脚本,该脚本接收a文件(例如./prog文件) 我需要解析文件并搜索字符串。这是我认为会起作用的,但似乎不起作用。该文件是每行包含50行的一个工作 @array = < >; print "Enter the word you what to match\n"; chomp($match = <STDIN>); foreach $line (@array){ if($match eq $

我正在创建一个perl脚本,该脚本接收a文件(例如./prog文件) 我需要解析文件并搜索字符串。这是我认为会起作用的,但似乎不起作用。该文件是每行包含50行的一个工作

@array = < >;   
    print "Enter the word you what to match\n";
    chomp($match = <STDIN>);        

    foreach $line (@array){
        if($match eq $line){
            print "The word is a match";
            exit
        }
    }
@array=<>;
打印“输入要匹配的单词\n”;
chomp($match=);
foreach$行(@array){
如果($match eq$行){
打印“单词匹配”;
出口
}
}

您正在选择用户输入,而不是文件中的行

他们无法匹配;一个以
\n
结尾,另一个没有。摆脱你的
chomp
应该可以解决这个问题。(或者,在循环中添加一个
chomp($line)

编辑,希望OP从下面的评论中注意到他的错误:

eq
更改为
=
不会“修复”任何东西;它打破了它。您需要使用
eq
进行字符串比较。您需要执行上述操作之一来修复代码

$a = "foo\n"; 
$b = "bar"; 
print "yup\n" if ($a == $b);
输出:


因此,在用户输入和chomp@array上不要使用chomp。我认为操作符读取文件并执行所有n\操作,要么同时执行,要么两者都不执行<代码>@array=<>不删除换行符。因此我将chomp添加到我的循环$match eq chomp($line)。但是当我运行它时,它似乎没有运行if($match-eq-chomp($line))也可以在列表上运行。例如:
chomp@array
@jengle-不,你没有。使用
==
是不正确的,你会发现不应该匹配的东西会突然出现。请参阅我对答案的编辑。字符串是否可能跨越两行?
foreach $line (@array){
    chomp($line);
    if($match eq $line){
        print "The word is a match";
        exit;
    }
}
$a = "foo\n"; 
$b = "bar"; 
print "yup\n" if ($a == $b);