我试图将@ arr3的范围值与@ arr4的范围值进行比较,但我没有得到所需的输出。请在下面的代码中建议修改以获得3,4,5,6,7,9,10,11,12,14,15
的输出(不重复值示例5和10)和总匹配= 11.
文件1:结果
3..7
9..12
14..17
文件2:注释
1..5
5..10
10..15
代码:
#!/usr/bin/perl
open ($inp1,"<result") or die "not found";
open ($inp2,"<annotation") or die "not found";
my @arr3=<$inp1>;
my @arr4=<$inp2>;
foreach my $line1 (@arr4) {
foreach my $line2 (@arr3) {
my ($from1,$to1)=split(/\.\./,$line1);
my ($from2,$to2)=split(/\.\./,$line2);
#print $from1;print "\n";
for (my $i=$from1;$i<=$to1 ;$i++) {
for (my $j=$from2;$j<=$to2 ;$j++) {
if ($i==$j) {
print "$i";`enter code here`print "\n";
}
}
}
}
答案 0 :(得分:0)
如果你的列表不是太大,你可以使用哈希,这是在Perl中实现“不重复”的最佳方法:
#!/usr/bin/perl
use warnings;
use strict;
my @result = ('3..4', '9..12', '14..17');
my @annotation = ('1..5', '5..10', '10..15');
my %cmp;
my $pass = 1;
for my $range (@result, undef, @annotation) {
$pass = 2, next unless $range;
my ($from, $to) = split /\Q../, $range;
for my $num ($from .. $to) {
$cmp{$num} = $pass if 1 == $pass or $cmp{$num};
}
}
my @output = sort { $a <=> $b } grep 2 == $cmp{$_}, keys %cmp;
print join(',', @output), "\nTotal matched: ", scalar @output, "\n";
答案 1 :(得分:0)
为我工作
#!/usr/bin/perl
open ($inp1,"<result") or die "not found";
open ($inp2,"<annotation") or die "not found";
my @arr3=<$inp1>;
my @arr4=<$inp2>;
foreach my $line1 (@arr4) {
foreach my $line2 (@arr3) {
my ($from1,$to1)=split(/\.\./,$line1);
my ($from2,$to2)=split(/\.\./,$line2);
for (my $i=$from1;$i<=$to1 ;$i++) {
for (my $j=$from2;$j<=$to2 ;$j++) {
$res = grep(/$i/, @result);
if ($i==$j && $res == 0) {
print "$i enter code here\n";
push(@result, $i);
}
}
}
}
}