我正在创建一个perl脚本,它接受我需要解析文件并搜索字符串的文件(例如./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
}
}发布于 2011-10-26 10:53:14
您正在处理用户输入,而不是文件中的行。
它们不能匹配;一个以\n结尾,另一个则不是。摆脱你的chomp应该可以解决这个问题。(或者,向循环中添加一个chomp($line) )。
$match = <STDIN>;或
foreach $line (@array){
chomp($line);
if($match eq $line){
print "The word is a match";
exit;
}
}编辑,希望操作员从下面的评论中注意到他的错误:
将eq更改为==不会“修复”任何东西;它会破坏它。您需要使用eq进行字符串比较。您需要执行上述操作之一来修复您的代码。
$a = "foo\n";
$b = "bar";
print "yup\n" if ($a == $b);输出:
yup
https://stackoverflow.com/questions/7898221
复制相似问题