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..

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";

thanks mate..!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.