954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Perl script to skip the first line and calculate the length

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 length.txt (0.03KB)
ghosh22
Junior Poster in Training
53 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
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";
k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
 

thanks mate..!!

ghosh22
Junior Poster in Training
53 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You