Perl 在FOR循环中打印唯一值

Perl 在FOR循环中打印唯一值,perl,Perl,我有两个文件myresult和annotation。两个文件中的数据显示为范围,但它们不是,这就是为什么我不能将其存储在数组中,我需要使用拆分运算符,以便在for循环中使用它并进行比较。现在我需要打印$i(myresult)和$j(annotation)中的所有公共值,而不需要重复(unique)。我没有得到什么条件以及如何实现它以获得所需的输出。我尝试使用%hash,但无法实现 我的结果 0..351 12..363 24..375 36..387 48..399 60..4

我有两个文件myresult和annotation。两个文件中的数据显示为范围,但它们不是,这就是为什么我不能将其存储在数组中,我需要使用拆分运算符,以便在for循环中使用它并进行比较。现在我需要打印$i(myresult)和$j(annotation)中的所有公共值,而不需要重复(unique)。我没有得到什么条件以及如何实现它以获得所需的输出。我尝试使用%hash,但无法实现

我的结果

0..351 
12..363  
24..375  
36..387  
48..399  
60..411  
.
.
注释

272..1042
1649..2629
3436..4752
4793..4975
5408..6022
6025..6252
.
.
代码:

#/usr/bin/perl

open($inp0,您不需要在这两个数组上进行迭代。如果范围的起点是升序,则可以使用以下代码:

#!/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)
}
!/usr/bin/perl
使用警告;
严格使用;
my@result=qw(4..208..1214..222..2927..2928..3540..50);
my@annot=qw(1..5 11..13 25..37 45..55);
我的$from=(拆分/\.\./,$result[0])[0];
我的$to=(拆分/\.\./,$result[-1])[1];
对于我的$i($from..$to){
如果grep在($i,$\n)内,则打印“$i\n”,@result
和grep-inside($i,$)@annot;
}
子内部{
我的($i,$range)=@;
我的($from,$to)=分割/\.\./,$range;

return($from您不需要迭代两个数组。如果范围的起点是升序,则可以使用以下代码:

#!/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)
}
!/usr/bin/perl
使用警告;
严格使用;
my@result=qw(4..208..1214..222..2927..2928..3540..50);
my@annot=qw(1..5 11..13 25..37 45..55);
我的$from=(拆分/\.\./,$result[0])[0];
我的$to=(拆分/\.\./,$result[-1])[1];
对于我的$i($from..$to){
如果grep在($i,$\n)内,则打印“$i\n”,@result
和grep-inside($i,$)@annot;
}
子内部{
我的($i,$range)=@;
我的($from,$to)=分割/\.\./,$range;

return($from将每个数据集转换为一个值数组

然后使用散列计算两个列表中匹配的uniq值:

#!/usr/bin/perl -w
use strict;
use warnings;
use autodie;

use List::MoreUtils qw(uniq);

my @result = do {
    #open my $fh, '<', "myresult";
    open my $fh, '<', \ "0..351\n12..363\n24..375\n36..387\n48..399\n60..411\n";
    map { my ( $min, $max ) = /\d+/g; ( $min .. $max ) } <$fh>;
};

my @annot = do {
    #open my $fh, '<', "myresult";
    open my $fh, '<', \ "272..1042\n1649..2629\n3436..4752\n4793..4975\n5408..6022\n6025..6252\n";
    map { my ( $min, $max ) = /\d+/g; ( $min .. $max ) } <$fh>;
};

my %count;
$count{$_}++ for uniq(@result), uniq(@annot);

print join( ' ', sort { $a <=> $b } grep { $count{$_} == 2 } keys %count ), "\n";

将每个数据集转换为值数组

然后使用散列计算两个列表中匹配的uniq值:

#!/usr/bin/perl -w
use strict;
use warnings;
use autodie;

use List::MoreUtils qw(uniq);

my @result = do {
    #open my $fh, '<', "myresult";
    open my $fh, '<', \ "0..351\n12..363\n24..375\n36..387\n48..399\n60..411\n";
    map { my ( $min, $max ) = /\d+/g; ( $min .. $max ) } <$fh>;
};

my @annot = do {
    #open my $fh, '<', "myresult";
    open my $fh, '<', \ "272..1042\n1649..2629\n3436..4752\n4793..4975\n5408..6022\n6025..6252\n";
    map { my ( $min, $max ) = /\d+/g; ( $min .. $max ) } <$fh>;
};

my %count;
$count{$_}++ for uniq(@result), uniq(@annot);

print join( ' ', sort { $a <=> $b } grep { $count{$_} == 2 } keys %count ), "\n";

您是否需要两个文件中的公共值的数量?您能否通过编辑问题中的内容告诉我们预期输出是什么。如果您清楚,则很容易为您提供解决方案。您是否需要两个文件中的公共值的数量?您能否通过编辑问题中的内容告诉我们预期输出是什么。如果您很清楚,那么就很容易为您提供解决方案。