Hi PerlSufi, your code is very fine for the example, but it does not scale up very well for more than two files (and the OP mentioned that there are multiple data files).

I would probably avoid storing each file into individual arrays, and try to process each file sequentially, and change the code to something like this (incomplete and obviously untested):

use strict; use warnings; my %data=(); for my $file (qw /file_1.txt file_2.txt, file_3.txt ... file_n.txt/) { open my $FH, "<", $file or die "could not open $file $!"; while (<$FH>) { chomp; my ($sz,$sum,@f) = split /\s+/; push @{$data{$sz}},$_ for @f; } close $FH; } # ...
Or possibly, if the files are passed as arguments to the script:
use strict; use warnings; my %data=(); for my $file (@ARGV) { open my $FH, "<", $file or die "could not open $file $!"; while (<$FH>) { # ... } # ... } # ...
or even (still assuming the files are passed as arguments):
use strict; use warnings; use autodie; my %data=(); while (<>) { chomp; # ... } #...
This latest solution might be used even if the argument passed to the script is not a list of files, but, say, the directory where they are stored:
use strict; use warnings; use autodie; my $stat_dir = shift; my %data = (); { local @ARGV = glob ("$stat_dir/*.*"); while (<>) { chomp; # ... } # ... }
Admittedly, the latest solutions look less robust and one might want to avoid them for production code. But are they really less robust? Hmm, if glob returns a list of files, then you basically know the files are there, the only thing that is lacking might be checking read privileges, no big deal.


In reply to Re^3: Appending arrays into the rows of a 2 dimension array by Laurent_R
in thread Appending arrays into the rows of a 2 dimension array by SoftwareGoddess

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.