in reply to Compare 2 files and create a new one if it matches

Load the first file into a hash instead of reading the a file over and over again.

And what's with using cat?!?!?!

#!/usr/bin/perl use strict; use warnings; my $File1 = '...'; my $File2 = '...'; my $File3 = '...'; my %keep; { open(my $fh_keys, '<', $File1) or die("Can't open key file \"$File1\": $!\n); while (<$fh_keys>) { chomp; $keep{$_} = 1; } } { open(my $fh_in, '<', $File2) or die("Can't open input file \"$File2\": $!\n"); open(my $fh_out, '>', $File3) or die("Can't create output file \"$File3\": $!\n"); while (<$fh_in>) { my ($key) = /^[^|]*\|([^|]*)/; print $fh_out $_ if $keep{$key}; } }

Update: Added missing chomp as per reply.
Update: Added missing "$" in "my $fh_in" and "my $fh_out".

Replies are listed 'Best First'.
Re^2: Compare 2 files and create a new one if it matches
by repellent (Priest) on Sep 20, 2008 at 18:44 UTC
    A chomp is needed for the hash keys.
Re^2: Compare 2 files and create a new one if it matches
by shawshankred (Sexton) on Sep 22, 2008 at 18:37 UTC
    Thanks a lot ikegami. I'll try this out and see how long it takes.