Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Comparing two text files

by space_monk (Chaplain)
on Jan 08, 2013 at 11:51 UTC ( #1012226=note: print w/ replies, xml ) Need Help??


in reply to Comparing two text files

Try something like this, which includes the suggestions by davido and Anonymous_Monk. As CountZero has suggested, you may also need to check some or all of the index values as arrays start at element zero in Perl ... :-)

use strict; use warnings; open my $bh, "<", $B_file or die "$0: open $B_file: $!"; my %bHash; # slurp text file B # could generate a unique key consisting of field 0 and 11, # but never mind. foreach (<$bh>) { my @text=split(/\t/); $bHash{$text[0]} = $text[11]; } close $bh; open my $ah, "<", $A_file or die "$0: open $A_file: $!"; # read in text file A foreach (<$ah>) { my @text = split(/\t/); if ($bHash{$text[3]} eq $text[11]) { # we have a match, print it print join( "\t", @text[1..3]),"\n"; } } close $ah;
A Monk aims to give answers to those who have none, and to learn from those who know more.


Comment on Re: Comparing two text files
Download Code
Re^2: Comparing two text files
by perlnoobster (Sexton) on Jan 08, 2013 at 17:25 UTC
    Thank you all for your advice! i've taken it aboard and it works great! I have one last question though, the value that could match the $text11 of file A can be found in several other columns such as 12,13,14,15 How can I adjust the original hash: <code>$bHash{$text[0]} = $text11; so that it includes text 12....15?<\code> is it possible to implement that? if so how? Thank you ! :) This doesnt work: $bHash{$text[0]} = $text11|$text12|$text13;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-12-25 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls