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

How about using 'Tie::File' module??
use strict; use warnings; use Tie::File; my %seen; tie my @file1, 'Tie::File', 'file.txt' or die; tie my @file2, 'Tie::File', 'file2.txt' or die; foreach (@file1) { chomp; $seen{$_}++; } for(@file2) { my $key = (split /\|/,$_)[1]; print "$_\n" if $seen{$key}; } untie(@file1); untie(@file2);
output: E|123|r|some|56|78|90 D|678|r|some|56|78|90 F|345|r|y|98|0|0

Replies are listed 'Best First'.
Re^2: Compare 2 files and create a new one if it matches
by GrandFather (Saint) on Sep 20, 2008 at 03:32 UTC

    Why complicate the problem by introducing tied files when you need make only one pass through the file in any case? You are simply adding overhead for no gain and obfuscating the the code into the bargain.


    Perl reduces RSI - it saves typing