0

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.

TLP
  • 66,756
  • 10
  • 92
  • 149
  • 2
    Missing $ for p in for statement. – GoinOff Mar 06 '14 at 18:37
  • 6
    If you added `use:strict` to your program, and declared all your variables with the `my` keyword, you would have gotten this warning when running your program: `Bareword "p" not allowed while "strict subs" in use at temp.pl line 33.` – Carl Anderson Mar 06 '14 at 18:43

1 Answers1

4

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
sartak
  • 653
  • 4
  • 17