I have two files, keys.txt and values.txt, in this form:

keys.txt

Joe Bob Sally Bob Fred
values.txt
5 3 7 1 1
There is a 1-to-1 mapping between lines in the first file and the second file. Let's say these are amounts of tips earned. If a name appears twice, the amounts accumulate. So these files indicate that Joe made $5, Bob made $4, Sally made $7, and Fred made $1.

In the real world there could be many millions of lines, but only a few hundred thousand keys (in this example, the names are the keys).

One can assume the two files are the same number of lines and are always in synch.

What is an elegant way to read these (big) files and print out the total amount, the average amount, the max amount, and the min amount for each key?

The obvious answer is to open each file and read the lines into a hash:

open(KEYS, "keys.txt") or die "error: $!"; open(VALS, "values.txt") or die "error: $!"; my ($key,$value); my (%tips_by_name, %av_by_name, %max_by_name, %min_by_name); while ($key=<KEYS>) { $value=<VALS>; chomp($key); chomp($value); $tips_by_name{$key} += $value; # code to do average, min, max here.. # I'm not asking about that part, my question is # more about the way of reading files } close(VALS); close(KEYS); # code to print out the results here... again # not part of my question at the moment.
Maybe a better way to do it would be to create an object for each key (the flyweight pattern?) but my question is as much about the way to read the files. Is there a more elegant way than this to read two files in lockstep? Is this a job for tie? (which I haven't used much if ever, so forgive me if that's a stupid question).

I know one answer would be to use the UNIX command line utility, paste, like this:

paste keys.txt values.txt > combined.txt
and then read the one file and do a split. Very simple. But I can't do that in this case and am looking for the best way to do it in Perl.

In reply to Summing repeated counts for items stored in separate file by dmorgo

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.