在perl中使用for循环和substr计算碱基的频率



我正试图使用for循环和substr函数来计算基数,但计数被关闭了,我不知道为什么!请帮忙!我必须在作业中使用这些函数。我哪里错了?这是我的代码:

use strict;
use warnings;
my $user_input = "accgtutf5";
#initalizing the lengths
my $a_base_total = 0;
my $c_base_total = 0;
my $g_base_total = 0;
my $t_base_total = 0;
my $other_total  = 0;
for ( my $position = 0; $position < length $user_input; $position++ ) {
    my $nucleotide = substr( $user_input, $position, 1 );
    if ( $nucleotide eq "a" ) {
        $a_base_total++;
    } elsif ( $nucleotide eq "c" ) {
        $c_base_total++;
    } elsif ( $nucleotide eq "g" ) {
        $g_base_total++;
    } elsif ( $nucleotide eq "t" ) {
        $t_base_total++;
    } else {
        $other_total++;
    }
    $position++;
}
print "a = $a_base_totaln";
print "c = $c_base_totaln";
print "g = $g_base_totaln";
print "t = $t_base_totaln";
print "other = $other_totaln";

我得到的输出是:

a=1
c=1
g=0
t=2
other=1

应该在什么时候:

a = 1
c = 2
g = 1
t = 2
other = 3

提前感谢!:)

您正在递增两次。

只需删除这一行:

$position++;

另外,我建议对字符进行迭代,而不是对位置进行迭代。

您的脚本可以简化为:

use strict;
use warnings;
my $user_input = "accgtutf5";
my %count;
for my $nucleotide (split '', $user_input) {
    $nucleotide = 'other' unless $nucleotide =~ /[acgt]/;
    $count{$nucleotide}++;
}
printf "%s = %dn", $_, $count{$_} // 0 for qw(a c g t other);

您将$position递增两次:一次在for,一次在循环结束时。卸下第二个$position++

最新更新