Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: how to compare two hashes with perl?

by colwellj (Monk)
on Nov 04, 2009 at 23:44 UTC ( [id://805098]=note: print w/replies, xml ) Need Help??


in reply to how to compare two hashes with perl?

try this
#!/usr/bin/perl use warnings; use strict; my %bow1 = (); my $file1 = shift; open (FILE1, "$file1")or die "could not open FILE1:$file1; # Open firs +t file while (<FILE1>) { my ($ID1, undef, undef, undef, $Seq1) = split; $bow1{$ID1} = $ID1; # your id is the key and can be got again } close FILE1; my $file2 = shift; open (FILE2, "$file2")or die "could not open FILE2:$file2"; # Open sec +ond file while (<FILE2>) { my ($ID2, undef, undef, undef, $Seq2) = split; if(defined($bow1{$ID2})){#IDs Match if($bow1{$ID2} eq $Seq2){ #both match so remove from the hash to avoid looking fo +r a correct one later delete $bow1{$ID2}; }else{ #id's dont match print "IDs exist but dont match ID:$ID2:Seq1:$bow1{$ID2}:Seq2:$ +Seq2:\n"; } }else{ #id not in list 1 print "ID only in file 2 ID:$ID2:$Seq1\n" } } close FILE2 #run through any remaing list one items that werent matched while((my $key,my $value) = each %bow1){ print "ID only in file 1 ID:$key:Seq:$value:\n"; }

Replies are listed 'Best First'.
Re^2: how to compare two hashes with perl?
by graff (Chancellor) on Nov 05, 2009 at 08:37 UTC
    I think that this part in your code:
    while (<FILE1>) { my ($ID1, undef, undef, undef, $Seq1) = split; $bow1{$ID1} = $ID1; # your id is the key and can be got again }
    should actually be like this:
    while (<FILE1>) { my ($ID1, undef, undef, undef, $Seq1) = split; $bow1{$ID1} = $Seq1; # ID is hash key, Seq is hash value }
      oops, well spotted.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-19 07:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found