2011-09-12 21 views
2

我有以下代码:在Perl的正则表达式编译使用未初始化值的

use strict; 
use warnings; 
use IO::File; 
use Bio::SeqIO; 

my ($file1) = $ARGV[0]; 
my ($file2) = $ARGV[1]; 

my $fh1 = IO::File->new("$file1")|| die "Can not create filehandle"; 
my $fh2 = IO::File->new("$file2")|| die "Can not create filehandle"; 

my @aligned_array =(); 

while(my $line1 = $fh1->getline){ 

    chomp($line1); 

    if (($line1 =~ /^match/)||($line1 =~ /^-/)) { 

     next; 

    } 
    else { 

     my @line_array = split(/\s+/, $line1); 
     push(@aligned_array, $line_array[9]); 

    } 

} 

my $fio1 = IO::File->new("> chimeric_contigs.txt")|| die "Can not create filehandle"; 
while(my $line2 = $fh2->getline) { 
    my $count = 0; 
    chomp($line2); 

    for my $aligned (@aligned_array) { 
     # print $line2.$aligned."\n"; 
     if ($line2 =~ m/$aligned/) { 

      $count++; 
     } 
    } 

    if ($count >= 2) { 

     print $fio1 $line2."\n"; 
    } 

} 

$fio1->close; 

和我不断收到同样的错误

使用未初始化值的正则表达式编译在/ gscuser/rfujiwar /箱/find_chimeric_contigs_blat.pl线41

这是线41:如果($ LINE2 =〜米/ $对齐/){

两个$ LINE2及$对齐的定义,因为我可以p他们没有问题。请帮忙。

+0

你打印它们时会得到什么? –

+0

,我期待 – Ryan

+0

Contig0.3Contig872.1 Contig0.3Contig872.1 Contig0.3Contig872.1 Contig0.3Contig872.1 Contig0.3Contig873.1等正确的琴弦......这就是取消注释#打印的结果$ LINE2 $对齐 “\ n”。 – Ryan

回答

4

(从评论转载,因为这最终解决了问题)是否定义了所有@aligned_array的元素?在41行之前放入next unless defined $aligned;

2

你会看到这个错误,如果$ line_array [9]未初始化的时候,你把它压@aligned_array:

my @line_array = split(/\s+/, $line1); 
    push(@aligned_array, $line_array[9]); 

换句话说,分裂没有发现10个空间分隔$ line1(和$#line_array小于9)中的元素。因此,不要将此行添加到数组中,或修复$ file1中的输入。