the original lists are sorted
This suggests a potential optimization, which would greatly reduce memory requirements and completely eliminate the need to sort your output, but at the cost of requiring a large number of filehandles:
- Open all the files at once.
- Grab the first line of each file and store it (along with an annotation to tell you which file it came from) in a heap or similar data structure. You could even use a plain array here, splicing each line into its proper (sorted) position as it's added.
- Retrieve the first (i.e., earliest-sorting) element from the heap. Parse it into the word and the number. If it's the word you're currently working on, add the number to the total; if it's a new word, write it to your output along with its total and start a new total for the new word.
- Read the next line from the file which provided the element you just handled and store that line in the heap.
- Repeat until all files run out of lines.
Since all input files are sorted and you're always processing the unprocessed line which sorts earliest, the output will automatically be sorted, without ever needing to have more than one line per input file in memory concurrently.
If you aren't able to allocate enough file handles at once to process the entire list of input files in one go, this technique can also be used to do the work in multiple stages - divide the files into N groups, get the combined sums for each group, and then use those combined sums as your new set of sorted input files.
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use Array::Heap::PriorityQueue::String;
my @file_names = qw( in1 in2 in3 );
my @fh;
my $heap = Array::Heap::PriorityQueue::String->new;
for (0 .. $#file_names) {
my $fn = $file_names[$_];
open my $new_fh, '<', $fn or die "Unable to open $fn: $!\n";
$fh[$_] = $new_fh;
add_from_fh_id($_);
}
my ($current_word) = split /\s+/, $heap->peek;
my $total = 0;
while (my $item = $heap->get) {
my ($word, $count, $fh_id) = split /\s+/, $item;
if ($word eq $current_word) {
$total += $count;
} else {
say "$current_word\t$total";
$current_word = $word;
$total = $count;
}
add_from_fh_id($fh_id);
}
say "$current_word\t$total";
sub add_from_fh_id {
my $fh_id = shift;
my $read_from = $fh[$fh_id];
my $next_line = <$read_from>;
if ($next_line) {
chomp $next_line;
$heap->add("$next_line $fh_id");
}
}
For my testing, I used the input files from
kcott's reply, sorted and renamed to 'in1', 'in2', and 'in3'. This gave me the output:
bar 70
blah 69
foo 146
life 42
word 81
yada 3
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.