Perl:如何打破嵌套循环?

时间:2013-11-10 13:47:18

标签: perl loops while-loop

我在Perl中遇到了一个问题。 我想用Regex扫描巨大的文本文件。 一切正常,但是当我在嵌套的while循环中找到Stop Codon时,第一个while循环接管,但它应该从找到的终止密码子的最后一个位置继续。有人能给我一个提示在哪里看?

我已经尝试了最后一个声明,但这不起作用。

use strict;
use warnings;
$|=1;

sub read_Fasta {
    my ($filename) = @_;
  open (FIN, "<$filename") or die "Bestand $filename niet te openen\n";
  my $sequence = "";
  while (my $line = <FIN>) {
      if ($line =~ /^\s*$/) {
      next;
    } elsif ($line =~ /^\s*#/) {
      next;
    } elsif ($line =~ /^>/) {
      next;
    } else {
      $sequence .= $line;
    }
  }
  close (FIN);
  $sequence =~ s/\s//g;
  return ($sequence);
}

print "Het vinden van ORF's in een organisme.\n\n";
print "Bestanden worden geladen vanuit de locatie C:\\ \n";
print "Voer bestandsnaam (inclusief extensie) in:\n";
my $path = <STDIN>;
chomp ($path);
$path = "C:\\$path";


my $DNA_seq = &read_Fasta($path);
chomp ($DNA_seq);
print "De DNA sequentie is gevonden\n";

print "voer de minimale lengte van de ORF in: \n"; 
my $minlengte_ORF = <STDIN>;
chomp ($minlengte_ORF);

if ($minlengte_ORF =~ /^[0-9]+$/ && $minlengte_ORF >= 1) {
print "De gevonden ORF's zijn:\n";

while(($DNA_seq =~ (/[ATG]TG/ig))) {
    my $startcodon = $&;
    my $pos_startcodon = pos($DNA_seq);
    $pos_startcodon -= 2;
    my $stringvanafstart = $';
    while($stringvanafstart =~ (/(TAA)|(TAG)|(TGA)/ig)){
        my $stopcodon = $&;
        my $lengte_ORF = length ($`);
        $lengte_ORF += 3; #vanwege meetellen startcodon.
        my $pos_stopcodon = pos ($stringvanafstart);
        $pos_stopcodon -= 2;
        if($lengte_ORF % 3 == 0){
            if($lengte_ORF >= $minlengte_ORF)   {
            print "de positie van het startcodon $startcodon is $pos_startcodon \n";
            print "de positie (vanaf het startcodon) van het stopcodon $stopcodon is $pos_stopcodon \n";
            print "de totale lengte van de ORF bedraagt $lengte_ORF basen\n";
            #Go back to first while loop, exclude everything till endposition. 
}
else{
    next;
}
}
else{
    next;
}
}
}
}
else{
    print "Invoer is onjuist, minimale lengte mag alleen getallen bevatten en/of moet groter zijn dan 0! \n";
}

1 个答案:

答案 0 :(得分:14)

Last采用可选标签:

OUTERLOOP: while ($expression) {
    while ($innerexpression) {
        ....
        last OUTERLOOP;
    }
}

其他选项是将循环放入函数并使用return,或将一些sentinel变量($ exitloop)设置为true并在两个循环中检查它。