in reply to How to combine multiple files together

How big are the other files? If you don't mind loading them into memory:
use 5.006; # Uses "open" syntax introduced in Perl 5.6.0. use strict; use warnings; my $output_file_name = shift(@ARGV); my $main_file_name = shift(@ARGV); my @other_file_names = @ARGV; my @data; foreach (@other_file_names) { open(my $fh_in, '<', $_) or die("Unable to open input file $_: $!\n"); while (my $line = <$fh_in>) { # Keep the \n on $line. chomp(my $chomped_line = $line); my @fields = split(/,/, $chomped_line); foreach my $idx (0..$#fields) { # Saves a reference instead of a copy to save memory. push(@{$data[$idx]{$fields[$idx]}}, \$line); } } } { open(my $fh_in, '<', $main_file_name) or die("Unable to open input file $main_file_name: $!\n"); open(my $fh_out, '>', $output_file_name) or die("Unable to open output file $output_file_name: $!\n"); while (my $line = <$fh_in>) { print $fh_out ($line); chomp($line); my @fields = split(/,/, $line); foreach my $idx (0..$#fields) { if ($data[$idx]{$fields[$idx]}) { foreach (@{$data[$idx]{$fields[$idx]}}) { print $fh_out ($$_); } } } } }

Untested. You didn't specify what to do And you should probably use Text::CSV_XS instead of splitting yourself.

Update: Fixed compilation errors.

Replies are listed 'Best First'.
Re^2: How to combine multiple files together
by xspikx (Acolyte) on Oct 17, 2005 at 16:09 UTC
    Well the main file can be from 50 lines to anywhere 2000 lines.
      That's tiny. But I don't load the main file into memory, only the other ones. I asked how big are the *other* files. If they're in the same range, you should have no problem.
        The other files can be from 2 lines to 4000 lines long. Depending on how many instances of the first field they have. But monstly only 1 file out of the 10 that would have such high number of lines.