If you're getting Out of memory errors even for my simple solution, you can try the following one. It processes files in bunches, saves the intermediate results, and in each step, it merges the new bunch into the saved result. It's much slower, but it can handle any number of files, provided you have enough memory to load at least two files at the same time.

The first parameter is the bunch size, setting it to 1 consumes least memory, but is the slowest. Setting it to the number of files makes it almost equivalent to the previous solution (it skips the merging phase completely).

For DNA length 18, size 10_000, and 1_000 files, the results were the following (my machine has 8GB of RAM):
old solutionbunch_size=100bunch_size=250bunch_size=500bunch_size=1000
Memory34.2%9.6%24.5%47.5%86.5%
Time2m 6s6m 20s4m 12s3m 8s2m 45s

The code:

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use List::Util qw{ sum }; use constant temp_file => "tmp.$$"; sub parse_block { my ($block) = @_; my @lines = split /\n/, $block; my $key = $lines[1]; my ($prefix, $counts) = $lines[3] =~ /^(\S+)\s((?:\d+\s*)+)/; my ($count) = $lines[3] =~ /count:(\d+)/; return { key => $key, counts => [ $counts =~ /\d+/g ], count => $count, prefix => $prefix, line0 => $lines[0], line2 => $lines[2], }; } sub process_first_bunch { my @files = @_; my %h; for my $file (@files) { print STDERR "$file\r"; open my $FIRST, '<', $file or die $!; while (my $block = <$FIRST>) { my $parsed = parse_block($block); $h{ $parsed->{key} } = [ @$parsed{qw{ line0 key line2 pref +ix }} ] unless exists $h{ $parsed->{key} }; push @{ $h{ $parsed->{key} } }, @{ $parsed->{counts} }; } } open my $TMP, '>', temp_file or die $!; for my $key (sort keys %h) { print_block($TMP, $h{$key}); } close $TMP or die $!; } sub process_next_bunch { my @files = @_; my %h; for my $file (@files) { print STDERR "$file\r"; open my $IN, '<', $file or die $!; while (my $block = <$IN>) { my $parsed = parse_block($block); $h{ $parsed->{key} } = [ @$parsed{qw{ line0 key line2 pref +ix }} ] unless exists $h{ $parsed->{key} }; push @{ $h{ $parsed->{key} } }, @{ $parsed->{counts} }; } } my %stats = ( ins => 0, merge => 0, 0 => 0 ); my @keys = sort keys %h; open my $TMP, '<', temp_file or die $!; my $new_temp = temp_file . $files[0]; open my $OUT, '>', $new_temp or die $!; while (my $block = <$TMP>) { my $parsed = parse_block($block); my $key = $parsed->{key}; while (@keys && $keys[0] lt $key) { # Insert. ++$stats{ins}; print_block($OUT, $h{ shift @keys }); } if (@keys && $key eq $keys[0]) { # Merge. ++$stats{merge}; shift @keys; push @{ $parsed->{counts} }, @{ $h{$key} }[ 4 .. $#{ $h{$k +ey} } ]; print_block($OUT, [ @$parsed{qw{ line0 key line2 prefix }} +, @{ $parsed->{counts} } ]); } else { # No change. ++$stats{0}; print {$OUT} $block; } } if (@keys) { # Add the rest. print_block($OUT, $h{$_}) for @keys; $stats{ins} += @keys; } close $OUT or die $!; rename $new_temp, temp_file or die $!; say STDERR "inserted: $stats{ins}, merged: $stats{merge}, kept: $s +tats{0}."; } sub print_block { my ($OUT, $details) = @_; my @lines = splice @$details, 0, 4; my $sum = sum(@$details); print {$OUT} join "\n", @lines; say {$OUT} "\t", join "\t", @$details, "count:$sum\n"; } local $/ = q(); my $bunch_size = shift; process_first_bunch(splice @ARGV, 0, $bunch_size); while (my @files = splice @ARGV, 0, $bunch_size) { process_next_bunch(@files); } say STDERR "Result: ", temp_file;

($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.