Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/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
从文件中提取行时,perl字符串比较失败。_Perl - Fatal编程技术网

从文件中提取行时,perl字符串比较失败。

从文件中提取行时,perl字符串比较失败。,perl,Perl,我的代码 #!/usr/bin/perl -w use strict; use warnings; my $codes=" "; my $count=0; my $str1="code1"; open (FILE, '/home/vpnuser/testFile.txt') or die("Could not open the file."); while($codes=<FILE>) { print($codes); if($codes eq

我的代码

#!/usr/bin/perl -w
use strict;
use warnings;


my $codes=" ";
my $count=0;
my $str1="code1";
open (FILE, '/home/vpnuser/testFile.txt') or  die("Could not open the file.");

while($codes=<FILE>)
{
        print($codes);
        if($codes eq $str1)
        {       
                $count++;
        }
}         
print "$count";
#/usr/bin/perl-w
严格使用;
使用警告;
我的$codes=“”;
我的$count=0;
我的$str1=“代码1”;
打开(文件“/home/vpuser/testFile.txt”)或死亡(“无法打开文件”);
而($code=)
{
打印($代码);
如果($codes eq$str1)
{       
$count++;
}
}         
打印“$count”;
这种比较总是失败的。我的testFile.txt包含一行简单的代码-code1 当我编写了一个单独的perl脚本,在脚本中声明了两个字符串,而不是从文件中获取时,eq操作符工作得很好。但是当我从文件中获取它时,有一个问题。请帮忙, 提前谢谢

如果不希望文件输入以返回字符结尾,请不要忘记输入文件

while(my $codes = <FILE>)
{
    chomp $codes;
while(我的$code=)
{
chomp$代码;
这可能就是字符串比较失败的原因

除此之外,kudus还感谢您在脚本的顶部添加和添加内容,就像您应该经常做的那样


我建议您在执行文件处理时也在顶部包含。它会自动为您提供执行多种操作(例如打开文件)时的详细错误消息,因此您不必记住在
语句中包含错误代码
$!
或文件名。

现在它开始工作了ks,但是@Miller,我的疑问是,在文件中,如果我只有一个单词“code1”,并且我没有按任何“enter”这样,在结束仪式时就不会有返回字符??在这种情况下,它应该可以在没有chomp仪式的情况下工作??但是如果文件只包含一个单词并且没有返回字符,那么,是的,在这种情况下,
chomp
是不必要的。然而,这就是为什么
chomp
是这样的这是一个很有用的函数,因为如果字符串不以输入记录分隔符结尾,它将不处理字符串。因此,使用它不会有什么坏处,而且这样做几乎总是有利的。