我正在对两行文本文件ref.txt
(参考)和log.txt
进行比较。但是任何一个文件中可能会有任意数量的空白行,我想忽略它们;我怎么能做到这一点?比较两个文件时,如何跳过(忽略)空白行?
ref.txt
one
two
three
end
log.txt的
one
two
three
end
会有输出没有不正确的日志行,在log.txt
与ref.txt
匹配等字样。
我喜欢什么伪代码来完成:
while (traversing both files at same time) {
if ($l is blank line || $r is blank line) {
if ($l is blank line)
skip to next non-blank line
if ($r is blank line)
skip to next non-blank line
}
#continue with line by line comparison...
}
我当前的代码:
use strict;
use warnings;
my $logPath = ${ARGV [0]};
my $refLogPath = ${ARGV [1]} my $r; #ref log line
my $l; #log line
open INLOG, $logPath or die $!;
open INREF, $refLogPath or die $!;
while (defined($l = <INLOG>) and defined($r = <INREF>)) {
#code for skipping blank lines?
if ($l ne $r) {
print $l, "\n"; #Output incorrect line in log file
$boolRef = 0; #false==0
}
}
对我来说,写下'除非$ line =〜/ \ S /'似乎更容易理解(至少对我来说)。 – 2012-07-20 10:34:46
@DaveCross我想你的版本可以确保在线读取有一些东西。总是有更多的方法来完成它INT Perl! – squiguy 2012-07-20 13:05:38
是的。在处理一个太多的“空行”实际上包含空格和/或制表符的文件之后,我切换到了我的方法。 – 2012-07-20 13:11:34