As far as I can see there is no need to
a) read in the whole file at once
b) paste the lines together
Your count won't change if you do the counting line by line - which would solve your memory problem. So it would be worth looking at the part of your program that needs the whole file as string to see, whether this could be changed too. If not, there is the proposition about using "tie" already.
Then, you can search for all valid characters at once and use a hash to collect and count them.
This would be a possible solution:
#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Data::Dumper;
my $filename = "dna.txt";
open(my $fh, "<", $filename) || die "could not open $filename: $!\n";
my %bases;
my $cnt_errors = 0;
while (<$fh>) {
# strip spaces
s/\s+//ig;
# collect results
my @results = ($_ =~ /[ACGT]/ig);
map { $bases{$_}++ } @results;
$cnt_errors += ( length($_) - scalar @results );
}
print Dumper(%bases);
print "Errors: $cnt_errors\n";
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|