否则就不能在perl中执行
我无法找出否则就不能在perl中执行,perl,Perl,我无法找出else部分未执行的原因: opendir (DIR_03, "$dir_01"."/"."$dir_02"."/"."$file"); @ANR=readdir(DIR_03); close(DIR_03); foreach my $anr_file (@ANR) { chomp($anr_file); if( $anr_file ne "traces.txt" || $anr_file ne "traces_system.txt" ||
else
部分未执行的原因:
opendir (DIR_03, "$dir_01"."/"."$dir_02"."/"."$file");
@ANR=readdir(DIR_03);
close(DIR_03);
foreach my $anr_file (@ANR) {
chomp($anr_file);
if( $anr_file ne "traces.txt" ||
$anr_file ne "traces_system.txt" ||
$anr_file ne "traces_SystemServer_WDT.txt") {
return("TraceFileNotFound");
} else {
return("TraceFileFound");
}
}
一般规则:不相等-或-组合几乎总是错误的。
(这里,如果一个ne
为假(相等),则其他的都被认为是真的,因此所有的都是真的;始终如此。)这:
if($anr_file ne "traces.txt" || $anr_file ne "traces_system.txt" ...
这几乎肯定不是你想要的。如果
$anr\u file
等于traces.txt
,则它不等于traces\u system.txt
,因此您的第一个子句将始终解析为true。它将始终为true,因为您使用的不是等于或
将其简化为文件a
、b
和c
,然后查看逻辑
如果查询$file='a'
并且
if ($file ne 'a' || $file ne 'b' || $file ne 'c') {...}
你有
if ($file ne 'a' || $file ne 'b' || $file ne 'c') {...}
if (false || true || true) {...}
对于$file
的任何值,这总是正确的。尝试将逻辑更改为eq
,并相应地更改if-else
块的内容
if($anr_file eq "traces.txt" || $anr_file eq "traces_system.txt" || $anr_file eq "traces_SystemServer_WDT.txt")
{
# Found it!
}
else
{
# Not Found
}
而且(从给出的示例来看),它可能更容易写为
if($anr_file eq“traces.txt”|$anr_file eq“traces_system.txt”|$anr_file eq“traces_SystemServer_WDT.txt”){找到文件…}或者{找不到文件}
。如果找不到,它不应该转到目录中的下一个文件而不是返回,打破循环?整个事情看起来有点混乱。你应该努力改进你的缩进。仅供参考,“$dir\u 01”。/“$dir\u 02”。/“$file”
写得更好。“$dir\u 01/$dir\u 02/$file”
。