in reply to Re: How to amend character count.
in thread How to amend character count.

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:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^3: How to amend character count.
by thanos1983 (Parson) on May 06, 2017 at 19:47 UTC

    Hello AnomalousMonk,

    No worries no problems, I am always open to read comments from monks which have greater knowledge.

    I am just commending to avoid confusion, OP on the open function has write mode > instead of read mode <.

    Regarding:

    I think that Peter Keystrokes wants to specify the input file on the command line, e.g.

    OP defined a file my $filename = 'counts.txt';, so I assumed that he is defining the file through the script not through the command line. But never the less I was not aware of Using the magic ARGV handle - <> in perlop. as anonymous point out. Thanks for the tip. :)

    Regarding:

    This loop counts 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.)

    I know some how that my results is wrong but I still can not understand why I can not link the lines that start with >hsa and why it only captures the ATG.... Can you provide me a bit more information? (Thanks in advance for your time and effort)

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      I am just commending to avoid confusion, OP on the open function has write mode > instead of read mode <.
      Because the OP opens this file with the intent of writing data to it, as shown in this code line:
      print $fh $count;
      So there is really nothing wrong with that.

      The OP code is reading from a file (passed as an argument to the program) and writing to another, opened explicitly with the open command.

        Hello Laurent_R,

        You are absolutely right. I was not aware of the (Variables related to filehandles):

        $ARGV Contains the name of the current file when reading from <> .

        Thanks again for clarification.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
      I still can not understand why I can not link the lines that start with >hsa and why it only captures the ATG.

      while (my $line = <>){ reads the line with the >hsa and then $line = <>; reads the next line.

        Hello Anonymous Monk,

        I finally got it, thank you for the explanation it is more clear now.

        I will update my answer just for personal future reference.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re^3: How to amend character count.
by Peter Keystrokes (Beadle) on May 07, 2017 at 21:52 UTC
    Thank you guys I am trying to absorb all your feedback (back and forth) as I'm pretty much a novice at this. I have been trawling the web taking bits of code and trying to make something work albeit not perfectly. At least here I can try to refine what little I have learnt. I appreciate this.