Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Compare 2 files and create a new one if it matches

by lamp (Chaplain)
on Sep 20, 2008 at 03:12 UTC ( [id://712684]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://712684]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-23 19:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found