Hello Peter Keystrokes,

I notices a few things on your code.

You have open (my $fh, '>', $filename) or die "Could not open '$filename' $!";. From the open documentation:

Simple examples to open a file for reading: open(my $fh, "<", "input.txt") or die "Can't open < input.txt: $!"; and for writing: open(my $fh, ">", "output.txt") or die "Can't open > output.txt: $!";

Also on your while loop you have while (my $line = <>){ without the file handler. From How can I read in an entire file all at once?:

while (<$input>) { chomp; # do something with $_ }

At the end of your code you are trying to write to a file print $fh $count;. You have opened the file in writing mode from the beginning but I am wondering how you are reading the file.

So I assume you meant to write something like that?

#!/usr/bin/perl use strict; use warnings; my $filename = 'counts.txt'; open (my $fh, '+<', $filename) or die "Could not open '$filename' $!"; my $count = ""; while (<$fh>) { chomp; if ($_ =~ /^>hsa/) { $count .= length $_; $count .= " "; } } print $fh $count . "\n"; close $fh or die "Could not close '$filename' $!"; __END__ >hsa_circ_0000001|chr1:1080738-1080845-|None|None ATGGGGTTGGGTCAGCCGTGCGGTCAGGTCAGGTCGGCCATGAGGTCAGGTGGGGTCGGCCATGAAGGTG +GTGGGGGTCATGAGGTCACAAGGGGGTCGGCCATGTG >hsa_circ_0000002|chr1:1158623-1159348-|NM_016176|SDF4 GGTGGATGTGAACACTGACCGGAAGATCAGTGCCAAGGAGATGCAGCGCTGGATCATGGAGAAGACGGCC +GAGCACTTCCAGGAGGCCATGGAGGAGAGCAAGACACACTTCCGCGCCGTGGACCCTGACGGGGACGGT +CACGTGTCTTGGGACGAGTATAAGGTGAAGTTTTTGGCGAGTAAAGGCCATAGCGAGAAGGAGGTTGCC +GACGCCATCAGGCTCAACGAGGAACTCAAAGTGGATGAGGAAA 49 54

Notice the open (my $fh, '+<', $filename) or die "Could not open '$filename' $!";. On this mode you can read and write to the file.

I do not know exactly why you want to read and write to your file, for me it would made more sense to read and print the output based on characters that they where counted but if this is what you desire.

I would prefer to do it like this, just personal preference nothing special.

#!/usr/bin/perl use strict; use warnings; my $filename = 'counts.txt'; open (my $fh, '+<', $filename) or die "Could not open '$filename' $!"; chomp(my @lines = <$fh>); for (@lines) { print $fh length($_) . " " if ($_ =~ /^>hsa/); } close $fh or die "Could not close '$filename' $!"; __END__ >hsa_circ_0000001|chr1:1080738-1080845-|None|None ATGGGGTTGGGTCAGCCGTGCGGTCAGGTCAGGTCGGCCATGAGGTCAGGTGGGGTCGGCCATGAAGGTG +GTGGGGGTCATGAGGTCACAAGGGGGTCGGCCATGTG >hsa_circ_0000002|chr1:1158623-1159348-|NM_016176|SDF4 GGTGGATGTGAACACTGACCGGAAGATCAGTGCCAAGGAGATGCAGCGCTGGATCATGGAGAAGACGGCC +GAGCACTTCCAGGAGGCCATGGAGGAGAGCAAGACACACTTCCGCGCCGTGGACCCTGACGGGGACGGT +CACGTGTCTTGGGACGAGTATAAGGTGAAGTTTTTGGCGAGTAAAGGCCATAGCGAGAAGGAGGTTGCC +GACGCCATCAGGCTCAACGAGGAACTCAAAGTGGATGAGGAAA 49 54

Update 3: Thanks to Anonymous Monk, AnomalousMonk, and Laurent_R I finally understand my mistakes and this is the updated recommended version.

#!/usr/bin/perl use strict; use warnings; my $count; while (<>) { if ($_ =~ /^>hsa/){ chomp (my $line = <>); $count .= length($line); $count .= " "; print "Count final: " . $count . "\n"; } } continue { close ARGV if eof; # reset $. each file } my $filename = 'counts.txt'; open (my $fh, '>', $filename) or die "Could not open '$filename' $!"; print $fh $count; close $fh or die "Could not close '$filename' $!"; __END__ $ perl test.pl test.txt Count final: 107 Count final: 107 251

And the way that I would try to resolve it.

#!/usr/bin/perl use strict; use warnings; my $filename = 'counts.txt'; open (my $fh, '>', $filename) or die "Could not open '$filename' $!"; chomp(my @lines = <>); for (@lines) { next if ($_ =~ /^>hsa/); print $fh length($_) . " "; } print $fh "\n"; close $fh or die "Could not close '$filename' $!"; __END__ $ cat counts.txt 107 251

Update: 4 Even better as Anonymous Monk point it out:

#!/usr/bin/perl use strict; use warnings; my $filename = 'counts.txt'; open (my $fh, '>', $filename) or die "Could not open '$filename' $!"; chomp(my @lines = <>); for (@lines) { print $fh length($_) . " " if ($_ =~ tr/ACGT//); } print $fh "\n"; close $fh or die "Could not close '$filename' $!"; __END__ $ cat counts.txt 107 251

Hope this helps.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Re: How to amend character count. by thanos1983
in thread How to amend character count. by Peter Keystrokes

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.