As suggested in the preceding replies, you've grabbed the wrong end of the stick and run with it. A better solution (for modest size data sets anyway) than the one you are struggling to implement is to read the files one at a time and merge the results in memory. Consider:

#!/usr/bin/perl use warnings; use strict; # First set up the sample files my @fileContents = ('a 1 b 2 c 3', 'a 5 d 3', 'a 1 x 4'); @ARGV = (); for (my $fileNum = 1; @fileContents; ++$fileNum) { my $fileName = "file$fileNum.txt"; open my $fileOut, '>', $fileName or die "Failed to create $fileNam +e: $!\n"; push @ARGV, $fileName; print {$fileOut} shift @fileContents; } # Now for the "real" code my %data; my $maxFile = @ARGV - 1; while (<>) { my %newData = split; $data{$_}[$maxFile - @ARGV] = $newData{$_} for keys %newData; } for my $key (sort keys %data) { $data{$key}[$maxFile] ||= 0; $_ ||= 0 for @{$data{$key}}; print "$key @{$data{$key}}\n"; }

Prints:

a 1 5 1 b 2 0 0 c 3 0 0 d 0 3 0 x 0 0 4

Note that most of the "tricky" code is to deal with getting the output data in the required format accounting for "missing" elements.

True laziness is hard work

In reply to Re: Open multiple file handles? by GrandFather
in thread Open multiple file handles? by onlyIDleft

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.