0
我想注释掉(实际上是打印到不同的文件)的行范围内的一个文件(数据)匹配,另一个文件(rangefile)说。所述rangefile是逐行,也就是说,如果我有以下到线注释掉不同的值范围在一个文件 - Perl的
2 4
7 8
我要评论出匹配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
前缀(数字)是的,这似乎是一个聪明的解决方案。 – PedroA 2014-11-20 19:07:28