in reply to How to amend character count.
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to amend character count.
by AnomalousMonk (Archbishop) on May 06, 2017 at 17:30 UTC | |
by thanos1983 (Parson) on May 06, 2017 at 19:47 UTC | |
by Laurent_R (Canon) on May 06, 2017 at 21:43 UTC | |
by thanos1983 (Parson) on May 07, 2017 at 18:26 UTC | |
by Anonymous Monk on May 06, 2017 at 20:49 UTC | |
by thanos1983 (Parson) on May 07, 2017 at 17:47 UTC | |
by Peter Keystrokes (Beadle) on May 07, 2017 at 21:52 UTC | |
|
Re^2: How to amend character count.
by Anonymous Monk on May 06, 2017 at 17:26 UTC | |
|
Re^2: How to amend character count.
by Anonymous Monk on May 07, 2017 at 19:29 UTC | |
by thanos1983 (Parson) on May 07, 2017 at 21:00 UTC |