http://qs321.pair.com?node_id=757874


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

tried with DB_BTREE now, but still getting the same results(wrong one). :( :( :(
  • Comment on Re^2: duplicates getting omitted while comparing values inside foreach.

Replies are listed 'Best First'.
Re^3: duplicates getting omitted while comparing values inside foreach.
by Corion (Patriarch) on Apr 16, 2009 at 07:40 UTC

    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{ $_ }}" };