我试图使用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_total\n";
print "c = $c_base_total\n";
print "g = $g_base_total\n";
print "t = $t_base_total\n";
print "other = $other_total\n";
我得到的输出是:
a=1
c=1
g=0
t=2
other=1
应该是:
a = 1
c = 2
g = 1
t = 2
other = 3
提前致谢! :)
答案 0 :(得分:0)
您正在两次增加$ position:一次在for
,一次在循环结束时。删除第二个$position++
。
答案 1 :(得分:0)
你递增了两次。
只需删除此行:
$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 = %d\n", $_, $count{$_} // 0 for qw(a c g t other);