我想注释掉(实际上是打印到不同的文件)一个文件(DATA)中与另一个文件(范围文件)匹配的一系列行。范围文件是逐行的,也就是说,如果我有以下行
2 4
7 8
我想在DATA中将注释标记为2,3,4和7,8。 到目前为止我所拥有的是:
#!/usr/bin/perl
use warnings;
use strict;
my $rangefile = $ARGV[0];
open (RANGE, $rangefile) or die "Couldn't open $rangefile: $!\n";
my %hash;
while ( <RANGE> ) {
my ($begin, $end) = split;;
$hash{$begin} = $end;
}
close RANGE;
my %seen;
while (<DATA>) {
if ( /^[^\d]/) { next }
# just split into an array because this file can have several fields
# but want to match 1st field
my @array = split;
foreach my $key (keys %hash) {
my $value = $hash{$key};
if ($array[0] >= $key && $array[0] <= $value) {
unless ( $seen{$array[0]} ++) {
print "#$_";
}
}
else {
unless ( $seen{$array[0]} ++) {
print;
}
}
}
}
__DATA__
1
2
3
4
5
6
7
8
9
10
但是这段代码要么打印#2,#3和#4,要么打印#7和#8,但不能同时打印两个范围。 通缉输出:
1
#2
#3
#4
5
6
#7
#8
9
10
答案 0 :(得分:2)
您的%hash
实际上应该包含您想要加#
前缀的键(数字)
#!/usr/bin/perl
use warnings;
use strict;
# my %hash = (2,4,7,8);
my ($rangefile) = @ARGV;
open (my $RANGE, "<", $rangefile) or die "Couldn't open $rangefile: $!\n";
my %hash;
while ( <$RANGE> ) {
my ($begin, $end) = split;
@hash{$begin .. $end} = ();
}
close $RANGE;
while (<DATA>) {
my ($num) = /^(\d+)/ or next;
s/^/#/ if exists $hash{$num};
print;
}
__DATA__
1
2
3
4
5
6
7
8
9
10