I tried to play with your code as well. I only stored the first block encountered for each DNA, but added the counts when finding another occurrence. The actual summing is done when printing the result.

Memory consumption seems to be about 50% of your solution or less, with the running time being the same or a bit shorter.

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use List::Util qw{ sum }; my %h; $/ = q(); while (my $block = <>) { my @lines = split /\n/, $block; my $key = $lines[1]; my ($count) = $lines[3] =~ /\s(\d+)/; unless (exists $h{$key}) { $block =~ s/\n\n?$//; $block =~ s/\s*\d+$//; $h{$key} = $block; } $h{$key} .= "\t$count"; } for my $key (sort keys %h) { my ($match) = $h{$key} =~ /((?:\d+\t*)+)$/; my @counts = $match =~ /\d+/g; my $sum = sum(@counts); say join "\t", $h{$key}, "count:$sum\n"; }

If you're interested, here's how I created the input data:

#!/usr/bin/perl use warnings; use strict; my ($n, $size, $dna_length) = @ARGV; my $template = << '__EOT__'; @ns %s + //%s %d __EOT__ open my $OUT, '>', "file$n" or die $!; for my $i (1 .. $size) { my $dna = join q(), map qw( A C G T )[ rand 4 ], 1 .. $dna_length; my $ignored = join q(), map +('A' .. 'Z')[ rand 26 ], 1 .. 4; my $count = 1 + int rand 20; printf {$OUT} $template, $dna, $ignored, $count; print {$OUT} "\n" unless $i == $size; }

Tested with 1000 files with size = 10000 where the DNA length was 6 or 10.

#!/bin/bash for i in {000..999} ; do create-file $i 10000 6 done
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

In reply to Re: merge multiple files giving out of memory error by choroba
in thread merge multiple files giving out of memory error by Anonymous Monk

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.