in reply to duplicates getting omitted while comparing values inside foreach.

Hashes don't have duplicate keys, you want a BTREE http://search.cpan.org/~pmqs/DB_File-1.820/DB_File.pm#Handling_Duplicate_Keys
  • Comment on Re: duplicates getting omitted while comparing values inside foreach.

Replies are listed 'Best First'.
Re^2: duplicates getting omitted while comparing values inside foreach.
by patric (Acolyte) on Apr 16, 2009 at 07:25 UTC
    tried with DB_BTREE now, but still getting the same results(wrong one). :( :( :(

      I'm not sure what your code is supposed to do. But if you want to check whether one list is a subset of the other, possibly with duplicates, I recommend using a hash of array references and "colliding" the elements out:

      my %elements; # open left file while (<$left>) { chomp; # extract key my $key = $_; $elements{ $key } ||= []; push @{$elements{ $key }}, $_; }; # open right file while (<$right>) { chomp; # extract key my $key = $_; if (! exists $elements{ $key }) { print "Missing totally from left file: $_\n"; } else { my $match = shift @{$elements{ $key }}; print "Matched: $_ against $match\n"; if (0 == @{$elements{ $key }}) { delete $elements{ $key }; }; }; }; for (keys %elements) { print "Only in left file: @{ $elements{ $_ }}" };