문제

I have written a perl program to find the percentage GC content in the given DNA string.But the program is executing the error situation(else part of conditional statement)

$dna = "AGTC";
$a = 0;
$g = 0;
$t = 0;
$c = 0;

for ($p = 0; p < length $dna; ++$p) {
    $ch = substr($dna,$p,1);
    if($ch eq 'A') {
            ++$a;
    } elsif($ch eq 'G') {
            ++$g;
    } elsif($ch eq 'T') {
            ++$t;
    } elsif($ch eq 'C') {
            ++$c;
    } else {
            print "error";
    }
}
$total = $a + $g + $t + $c;
$gc = $g + $c;
$percentagegc = ($gc/$total) * 100;
print "percentage gc content is = $percentagegc";

Please help.

도움이 되었습니까?

해결책

You're missing a $ in one of your usages of $p in this line:

for($p = 0;p < length $dna;++$p)
           ^ -- here

Fixing that and running your script I correctly get:

percentage gc content is = 50
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top