从数组返回匹配的元素

时间:2016-11-21 19:11:10

标签: perl

如果匹配由标量表示的模式,我想返回数组的元素。我想避免循环,所以我尝试了以下方法:

使用List :: Util'first';
my $ match = first {/ $ pattern /} @list_of_strings;

$ match变量没有返回值,即使我知道它与数组中的一个元素完全匹配。我做错了什么?

my @amplicon_exon = ();
        open(TXT5, "$amplicon_exon");
            while (<TXT5>){
                my $file_line = $_;
                $file_line =~ s/\s+\z//g;
                push (@amplicon_exon, $file_line);   
            }
        close(TXT5);

foreach (@amplicon_exon){
    chop($_);
}

my @matrix_lines = ();
        open(TXT5, "$matrixfile");
            while (<TXT5>){
                my $matrixlineentry = $_;
                $matrixlineentry =~ s/\s+\z//g;
                push (@matrix_lines, $matrixlineentry);   
            }
        close(TXT5);

        foreach (@matrix_lines){
    chop($_);
}
   my @failedamplicons = ();     
for my $vcf_file ( @vcf_files ) {

        $vcf_file =~ m|([^/]+)_annotated.vcf$| or die "Can't extract Sample ID";
         my $sample_id = $1;

         my @myuniquearray = ();
         my @amplicon_array = ();

        my $entire_matrix_header = $matrix_lines[0]; print "The matrix header is ".$entire_matrix_header."\n";
        $entire_matrix_header =~ s/\s+\z//g;
        my @matrix_headers = split (/\t/, $entire_matrix_header);
        push @matrix_headers, "endoffile";
        my $matrix_column_number = "";
        for (0..@matrix_headers){
                my $onco_matrix_header = $matrix_headers[$_];
                $onco_matrix_header =~ s/\s+\z//g;

                if ((length $onco_matrix_header > 0) && (index($sample_id, $onco_matrix_header) != -1)) {
                        $matrix_column_number = $_;
                        print "The matrixcolumnnumber is ".$matrix_column_number."\n";
                }
        }

        #print " The matrixcolumnnumber is ".$matrixcolumnnumber."\n";        


        for (1..@matrix_lines-1){
                my @matrix_values = split (/\t/, $matrix_lines[$_]);
                if ( ($matrix_values[$matrix_column_number]<201) && ($matrix_values[$matrix_column_number]>-1) ) {

                        my $f_amp = $matrix_values[1];#if ( grep( /^$value$/, @array ) )
                        print Dumper($f_amp, \@amplicon_exon);
                        my $match = first { /$f_amp/ } @amplicon_exon; print "#############the match is $match\n";
                        my @parts = split /:/, $match;
                        my $exon_amp = $parts[1]; my $gene_res = $parts[2];
                        print "less than 200 exists";

                         my @total_amps = ($run_folder, $sample_id, $gene_res, $exon_amp, $matrix_values[$matrix_column_number], $f_amp);
                         my $failedamplicon = join "\t", @total_amps;


                        push (@failedamplicons, $failedamplicon);
                }

        }

        for (0..@failedamplicons-1){
                my $failedamplicons = $failedamplicons[$_];
                open (MYFILE, ">>$failed_amps_output");##opens files with header and adds the rest of the lines.
                print MYFILE $failedamplicons."\n";
                close (MYFILE);
                }

        }

示例数据:

@amplicon_exon行:

ON_MAP2K1_1:2:MAP2K1
OCP1_MAP2K1_1:3:MAP2K1
OCP1_MAP2K1_2:6:MAP2K1

$ f_amp例子:

ON_MAP2K1_1
OCP1_MAP2K1_1
OCP1_MAP2K1_2

matrixfile

Gene   Target         barcode-1 barcode-2 barcode-3 barcode-4 barcode-5
MOTOR  Focus_MTOR_1    22        786       123       456       456
JAK1   OCP1_JAK1_2     345       45         342       23       432
ALT    OCP1_ALK_3      43        456        23        3         56

1 个答案:

答案 0 :(得分:1)

您尚未展示的部分程序出现问题

这是一个简短的程序,它使用您作为示例提供的@amplicon_exon值,并为first的每个值转储$f_cmp调用的结果。它的工作原理

也许这会让你感到害怕你没有做错什么?

use strict;
use warnings 'all';

use List::Util 'first';
use Data::Dump;

# Using sample @amplicon_exon lines

my @amplicon_exon = qw/
    ON_MAP2K1_1:2:MAP2K1
    OCP1_MAP2K1_1:3:MAP2K1
    OCP1_MAP2K1_2:6:MAP2K1
/;

# Using $f_amp examples:

for my $f_amp (qw/
    ON_MAP2K1_1
    OCP1_MAP2K1_1
    OCP1_MAP2K1_2 / ) {

    my $match = first { /$f_amp/ } @amplicon_exon;

    dd $match;
}

输出

"ON_MAP2K1_1:2:MAP2K1"
"OCP1_MAP2K1_1:3:MAP2K1"
"OCP1_MAP2K1_2:6:MAP2K1"


我的风险为5英镑@matrix_values包含换行符