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.
In reply to Re: How to combine multiple files together
by ikegami
in thread How to combine multiple files together
by xspikx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |