所以我有我的第二個循環下面的問題。Perl:For循環退出條件(最後)不按預期方式工作
第一個for循環在ARRAY中查找第一個ATG實例。
第二個for循環應該報告在第一個ATG之後的ARRAY中的第一個TAA,TAG或TGA實例。但相反,它報告了ARRAY中最後一個TAA,TAG或TGA的位置。我不知道爲什麼退出循環的條件並不能防止這個問題,也不知道如何解決這個問題。
任何提示將不勝感激。
my @test_srsrspsp = ("CCC", "ATG", "ATG", "CGC", "TAA", "TAG");
sub orf_length {
#index scalars
my $rf0_start;
my $rf0_end;
#index value counter
my $i = 0;
#finds first appearance of ATG in array
for (@_) {
$rf0_start = $i if $_ eq 'ATG';
last if (defined $rf0_start);
$i++;
}
#only looks for TAG, TAA, or TGA if ATG was found first
if (defined $rf0_start) {
#reset counter
$i = 0;
#is supposed to return the index value of the first appearance of TAG, TAA, or TGA
#that has an index value larger than that of ATGs but instead returns the index value
#of the last TAA, TAG, or TGA
for (@_) {
$rf0_end = $i if $_ =~ /TA(G|A)|TGA/;
if ((defined $rf0_end) > $rf0_start) {
last;
}
$i++;
}
}
#reports positions of found values and the number length of the sequence between them
if (defined($rf0_end and $rf0_start)) {
my $length = ($rf0_end - $rf0_start + 1) * 3;
print "Start Codon after pos: $rf0_start \n";
print "End Codon at pos: $rf0_end \n";
print "First ORF of \[email protected]_ \nhas length: $length \n";
} else {
print "No ORF found in @_\n";
}
}
我也試過使用for循環的不同版本沒有成功。
for (@_) {
$rf0_end = $i if $_ =~ /TA(G|A)|TGA/;
last if ((defined $rf0_end) > $rf0_start);
$i++;
}