I created a quick solution to your problem that loads all of the data from every file into a 3D array. It then outputs all of the data into separate files for each variable. See below.

use strict; my @file = qw(file1.txt file2.txt file3.txt); # read all files into one big 3D matrix my $data = []; for my $i (0 .. $#file) { open DATA, '<', $file[$i] or die; while (<DATA>) { chomp; my @line = split /\s+/, $_; my $linenum = shift @line; $data->[$_][$linenum][$i] = shift @line for (0..$#line); } } # output to files for each variable for my $n (0 .. $#$data) { open FILE, '>', "var$n.txt" or die; select FILE; for my $linenum (0 .. $#{$data->[$n]}) { print join "\t", $linenum, @{$data->[$n][$linenum]}, "\n"; } close FILE; }

I fed the above code with file1.txt, file2.txt, and file3.txt all identical to the file below:

0 0 1 2 3 4 5 1 0 1 2 3 4 5 2 0 1 2 3 4 5

Which then produced the following files:

var0.txt

0 0 0 0 1 0 0 0 2 0 0 0

var1.txt

0 1 1 1 1 1 1 1 2 1 1 1

var2.txt

0 2 2 2 1 2 2 2 2 2 2 2

var3.txt

0 3 3 3 1 3 3 3 2 3 3 3

var4.txt

0 4 4 4 1 4 4 4 2 4 4 4

var5.txt

0 5 5 5 1 5 5 5 2 5 5 5

I'm sure that there might be a more memory efficient method, perhaps using PDL. However, if your files are not extremely large, then you should be fine.


In reply to Re: append to a line arrays read from N different input files by dogz007
in thread append to a line arrays read from N different input files by ioana007

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.