Oh, but dmorgo, you can do a simple UNIX-like paste. First construct a paste subroutine similar to the one below:
sub paste { my @fh = @_; return sub { chomp( my @line = map { scalar(<$_>) } @fh ); return join ' ', @line; } }
Notice that this sub returns another anonymous sub (called an iterator) that reads one line at a time from each of the input file handles and returns them "pasted" together, separated only by a little whitespace for easy splitting. To take advantage of this code, simply open your files and paste them together.
open my $keys, "keys.txt" or die "couldn't open keys.txt"; open my $vals, "vals.txt" or die "couldn't open vals.txt"; my $combined = paste($keys,$vals); print $combined->(), "\n" for 0..4;
Using the example keys and vals given in your question, the output of this reads:
Joe 5 Bob 3 Sally 7 Bob 1 Fred 1
If the deref syntax of $combined->() isn't quite to your taste, then whip up a little syntactic sugar to help the medicine go down:
sub NEXTLINE { $_[0]->() } print NEXTLINE($combined), "\n" for 0..4;
There are two benefits to this method that I can think of immediately. One is that it allows you to use a sub similar to the UNIX paste command with which you are already familiar. The second is that it avoids both the generation of a potentially huge %combined hash and the generation of a potentially huge combined.txt file. The concept of an iterator is not my original idea, but I use it whenever possible to keep my code clean and orderly. See Mark Jason Dominus's excellent text Higher Order Perl for more tricks and details on iterators.

In reply to Re: Summing repeated counts for items stored in separate file by dogz007
in thread 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.