in reply to How do I Extract contents from given input files and merge into one text file based on Unique keys present in input files

Hello Monks, I could merge the input files in row wise, i am not able to merge it in column wise, as expected, Please help.

Below code i wrote which will merge row wise

use strict;
use warnings;
opendir(DIR, '.') or die "Can't open .$!";
my @inputfiles = sort {$a <=> $b} (grep(/\.txt$/,readdir(DIR)));
my $Outputfilename="Output.txt";
my $file;
print @inputfiles;
open (OUTPUT,">$Outputfilename");
foreach $file (@inputfiles)
{
open (INPUT,$file);
while(<INPUT>)
{
print OUTPUT $_;
}
}
close(OUTPUT);
  • Comment on Re: How do I Extract contents from given input files and merge into one text file based on Unique keys present in input files

Replies are listed 'Best First'.
Re^2: How do I Extract contents from given input files and merge into one text file based on Unique keys present in input files
by dwm042 (Priest) on Sep 02, 2009 at 17:18 UTC
    lnin,

    bart above really does have the answer to this question. You can't easily read data and then immediately write data any way you please. To write the data column-wise or anyway-wise, it's best to:

    • read all the data and store it.
    • write the data out in the format you want.
    Given that you are wanting to write out the data in terms of iterations and line numbers, the data can be stored in an array (as pairs of numbers, for example). The index to the array can be the number of the iteration.