A few points about your code spring to mind.
Inside the while loop you have if and else code branches. The first thing you do in each is chomp the string. It would make more sense to do the chomp before the if.
You assign $_ to $s. If you are doing this for readability then choose a variable name that is more meaningful, otherwise just operate on $_.
You are having difficulty with multiple-line data because you operate on each data line individually rather than accumulating the data lines then processing them all once you reach the next header.
What do you do to process the last data item when you reach end of file?
I think this code will do what you want. I have moved the process of getting the data length and printing the item into a subroutine.
use strict; use warnings; my $header = q{}; my $dataAccumulator; while( <DATA> ) { chomp; if( m{^>} ) { printDataItem() if $header; ( $header ) = m{^.(\S+)}; $dataAccumulator = q{}; } else { $dataAccumulator .= $_; } } printDataItem(); sub printDataItem { print qq{>$header length=}, length $dataAccumulator, qq{\n$dataAccumulator\n}; } __DATA__ >IDnumber1 length=350 AGCTG AAGTCGCT >IDnumber2 length=350 AGAACGT ACC >IDnumber3 length=350 AGC ACTTCGCTAACT
The output.
>IDnumber1 length=13 AGCTGAAGTCGCT >IDnumber2 length=10 AGAACGTACC >IDnumber3 length=15 AGCACTTCGCTAACT
I hope this is of use.
Cheers,
JohnGG
Update: Corrected typo.
In reply to Re: chopping new line while counting length
by johngg
in thread chopping new line while counting length
by sugar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |