Perl 如何在不重复的情况下比较2个重叠范围
我试图将@arr3的范围值与@arr4的范围值进行比较,但没有得到所需的输出。请建议我修改以下代码,使输出为3,4,5,6,7,9,10,11,12,14,15,而不重复示例5和10中的值,且总匹配=11 文件1:结果Perl 如何在不重复的情况下比较2个重叠范围,perl,Perl,我试图将@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 代码: 如果列表不太大,可以使用散列,这是实现无需在Perl中重复的最佳方法: #!/usr/bin/perl use warnings; use strict; my @res
3..7
9..12
14..17
文件2:注释
1..5
5..10
10..15
代码:
如果列表不太大,可以使用散列,这是实现无需在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";
为我工作
#!/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);
}
}
}
}
}
看。你从上一个问题得到的答案有什么问题?在一个文件中重复输入失败,而另一个文件中不匹配:my\@result='9..12','14..17';我的\@注释='1..5','5..10','10..15'@ColinHipps:谢谢,修复了。我还有1个数组arr2=4,5,2,现在我只想在arr2的元素大于3时打印$i值。上面的代码给出的输出是-3,4,5,6,7,9,10,11,12,14,15,但是在实现了这个新条件之后,它应该给出3,4,5,6,7,9,10的输出。我没有达到实施条件的目的,我已经尝试过了,但没有达到预期的效果@鬼魂
#!/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);
}
}
}
}
}