Hi thanos1983. Some comments on your post which I hope may avoid future confusion.
You have open (my $fh, '>', $filename) or die "Could not open '$filename' $!";. From the open documentation ...
Except for the use of $filename in the OPed code instead of repeated literal strings (which I think is good practice in the interest of DRYness) and the inclusion of the mode in the error message, the OPed code is essentially the same as the documented examples. I don't understand the point you want to make.
Also on your while loop you have while (my $line = <>){ without the file handler.
I think that Peter Keystrokes wants to specify the input file on the command line, e.g.
perl script.pl input.file
IOW, to read and write to separate files rather than appending to the input file as your final code example does | both your code examples do.
while (<$fh>) { chomp; if ($_ =~ /^>hsa/) { $count .= length $_; $count .= " "; } }
Update:for (@lines) { print $fh length($_) . " " if ($_ =~ /^>hsa/); }
This loop counts | These loops count the characters in the lines beginning '>hsa' rather than in the single string of the 'ACTG...' line that follows. The counts of 49 and 54 characters, respectively, for the two example data records reflect this. (And again, these counts are appended to the end of the input file rather than written to a separate file.)
Please believe that these comments are not meant to "pile on," but are only offered in the hope of smoothing the way a bit for future Seekers of Perl Wisdom.
Give a man a fish: <%-{-{-{-<
In reply to Re^2: How to amend character count.
by AnomalousMonk
in thread How to amend character count.
by Peter Keystrokes
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |