来自正则表达式的 Perl 字符串与创建的字符串不同 - 中断 fgrep
Perl string from regex is different from created string - breaks fgrep
我有一段代码正在检查文件中的文件名 givne 是否存在于主列表中,但它一直说该字符串不存在 - 但如果我用其中的文件名 - 手动输入 - 它工作正常。
来自正则表达式的变量与我声明的变量不同(它们看起来相同)。我检查了这个,但是通过 Perl 的自动类型解析,我不知所措,无法理解问题所在。
...
open (my $fh_in, $in_file) or die "Could not open file $in_file $!";
seek ($fh_in, 5995, 0);
my $line = readline ($fh_in);
$line =~ /"([^"]*)"/; # extracts the filename from a Filename="..." field
my $adf_file = ;
if (fgrep { /$adf_file/ } "masterlist.txt" ) {
print "The file is in the master list\n";
} else {
print "It isn't\n";
}
它说该文件不在主列表中。
如果我这样做:
my $testst = "File_I_Want";
fgrep { /$testst/ } ...
它说文件在主列表中。
另外如果我再比较一下:
if ($testst eq $adf_file) {
print "variables are the same\n";
} else {
print "variables are not the same\n";
}
正在打印它们不同但看起来相同。
在此先感谢您的帮助。
如果添加
,将有助于查看两个字符串之间的差异
use Data::Dumper;
$Data::Dumper::Useqq = 1;
print Dumper $adf_file;
设置 Useqq
选项使 Data::Dumper
以双引号样式显示字符串,对所有不可打印的字符使用转义序列。
更好的是 Data::Dump
,它只需要
use Data::Dump;
dd $adf_file;
做同样的事情。它还会生成更易读的嵌套数据结构显示,但它不是核心模块,可能需要安装。
我有一段代码正在检查文件中的文件名 givne 是否存在于主列表中,但它一直说该字符串不存在 - 但如果我用其中的文件名 - 手动输入 - 它工作正常。
来自正则表达式的变量与我声明的变量不同(它们看起来相同)。我检查了这个,但是通过 Perl 的自动类型解析,我不知所措,无法理解问题所在。
...
open (my $fh_in, $in_file) or die "Could not open file $in_file $!";
seek ($fh_in, 5995, 0);
my $line = readline ($fh_in);
$line =~ /"([^"]*)"/; # extracts the filename from a Filename="..." field
my $adf_file = ;
if (fgrep { /$adf_file/ } "masterlist.txt" ) {
print "The file is in the master list\n";
} else {
print "It isn't\n";
}
它说该文件不在主列表中。 如果我这样做:
my $testst = "File_I_Want";
fgrep { /$testst/ } ...
它说文件在主列表中。
另外如果我再比较一下:
if ($testst eq $adf_file) {
print "variables are the same\n";
} else {
print "variables are not the same\n";
}
正在打印它们不同但看起来相同。
在此先感谢您的帮助。
如果添加
,将有助于查看两个字符串之间的差异use Data::Dumper;
$Data::Dumper::Useqq = 1;
print Dumper $adf_file;
设置 Useqq
选项使 Data::Dumper
以双引号样式显示字符串,对所有不可打印的字符使用转义序列。
更好的是 Data::Dump
,它只需要
use Data::Dump;
dd $adf_file;
做同样的事情。它还会生成更易读的嵌套数据结构显示,但它不是核心模块,可能需要安装。