0

hi guyz..how r u? I am facing a problem with a simple Perl script. I want my code to calculate the length/number of letters present in a text file (which is the input). But it should skip the line starting with '>'. The input used is given below (also attached).

>blast1
CGTTAC
GGCTAC

I wrote the following code:

#!usr/bin/perl -w

print "Print your file name with location\n";

$dnafile=<STDIN>;

chomp $dnafile;

open (DNA, $dnafile);



while ($dna=<DNA>) {
 chomp ($dna);

  if ($dna=~/^(A|C|G|T)/) {


@dna=split ('', $dna);

 
$length= length ($dna);

   print "Length is $length\n";
 }
}

But it shows the output like this:
Length is 6.
Length is 6.
But I want it Length is 12 (because when I use an input text file with hundreds of lines and letters, it would be harder to count. But the format of the text file will remain same.
It would be great also if you could point out my mistakes here, thus helping me to learn from my mistakes.
Many thanks in advance..

Attachments
>blast1
CGTTAC
GGCTAC
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by ghosh22
1

But I want it Length is 12

So you must add the length value recursively in $length.
Below i slightly change you code, try this....

#!usr/bin/perl -w

print "Print your file name with location\n";
$dnafile=<STDIN>;
chomp $dnafile;

open (DNA, $dnafile) || die "Cannot open the file : $!";

while ($dna=<DNA>)
{
	chomp ($dna);
	### Check Starting not equals to '>' letter
	if ($dna=~/^[^>]/)
	{
		@dna=split ('', $dna);
		### Recursively count the length.
		$length += length ($dna);
	}
}

### Get the final length 
print "Length is $length\n";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.