in reply to chopping new line while counting length

[ Link to other thread for reference: printing the length of string variables ]

The following uses a single-line lookahead, and it happens to only keep one sequence in memory at a time.

use strict; use warnings; my $fh = \*DATA; my $line = <$fh>; while (defined($line)) { my $head = $line; $line = <$fh>; die("Premature EOF\n") if !defined($line); chomp( my $seq = $line ); for (;;) { $line = <$fh>; last if !defined($line) || $line =~ /^>/; chomp( $seq .= $line ); } my $len = length($seq); $head =~ s/(length=)\d+/$1$len/; print("$head$seq\n"); } __DATA__ >IDnumber1 length=350 AGCTG AAGTCGCT >IDnumber2 length=350 AGAACGT ACC >IDnumber3 length=350 AGC ACTTCGCTAACT