我有两个文件 myresult 和 annotation。两个文件中的数据似乎是范围,但事实并非如此,这就是为什么我无法将其存储在数组中,我需要使用拆分运算符,以便我可以在 for 循环中使用它并进行比较。现在我需要打印 $i(myresult) 和 $j(annotation) 中的所有常见值,而不重复(唯一)。我不知道哪个条件以及如何实现它以获得所需的输出。我尝试使用 %hash 但无法实现。
myresult
0..351
12..363
24..375
36..387
48..399
60..411
.
.
注解
272..1042
1649..2629
3436..4752
4793..4975
5408..6022
6025..6252
.
.
CODE:
#!/usr/bin/perl
open( $inp0, "<myresult" ) or die "not found";
open( $inp2, "<annotation" ) or die "not found";
open( $out, ">output" );
my @arr2 = <$inp0>;
my @arr4 = <$inp2>;
my $sum1 = 0;
foreach my $line1 (@arr2) {
my ( $from1, $to1 ) = split( /\.\./, $line1 );
foreach my $line2 (@arr4) {
my ( $from2, $to2 ) = split( /\.\./, $line2 );
for ( my $i = $from1; $i <= $to1; $i++ ) {
for ( my $j = $from2; $j <= $to2; $j++ ) {
if ( $i == $j ) {
print $out "$i \n";
$sum1++;
}
}
}
}
}
print "Unique values = $sum1";
您不需要迭代这两个数组。如果范围的起点是升序的,则可以使用以下代码:
#!/usr/bin/perl
use warnings;
use strict;
my @result = qw( 4..20 8..12 14..22 22..29 27..29 28..35 40..50 );
my @annot = qw( 1..5 11..13 25..37 45..55 );
my $from = (split /\.\./, $result[0])[0];
my $to = (split /\.\./, $result[-1])[1];
for my $i ($from .. $to) {
print "$i\n" if grep inside($i, $_), @result
and grep inside($i, $_), @annot;
}
sub inside {
my ($i, $range) = @_;
my ($from, $to) = split /\.\./, $range;
return ($from <= $i and $i <= $to)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)