Counting the frequencies of bases using for loop and substr in perl -
Counting the frequencies of bases using for loop and substr in perl -
i'm trying count number of bases using loop , substr function counts off , i'm not sure why! please help! have utilize these functions in assignment. going wrong? here code:
use strict; utilize warnings; $user_input = "accgtutf5"; #initalizing lengths $a_base_total = 0; $c_base_total = 0; $g_base_total = 0; $t_base_total = 0; $other_total = 0; ( $position = 0; $position < length $user_input; $position++ ) { $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";
the output i'm getting :
a=1 c=1 g=0 t=2 other=1
when should be:
a = 1 c = 2 g = 1 t = 2 other = 3
thanks in advance! :)
you're incrementing twice.
simply remove line:
$position++;
also, instead of iterating on position, suggest iterating on character.
your script can simplified just:
use strict; utilize warnings; $user_input = "accgtutf5"; %count; $nucleotide (split '', $user_input) { $nucleotide = 'other' unless $nucleotide =~ /[acgt]/; $count{$nucleotide}++; } printf "%s = %d\n", $_, $count{$_} // 0 qw(a c g t other);
perl for-loop substr
Comments
Post a Comment