Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: duplicates getting omitted while comparing values inside foreach.

by Corion (Patriarch)
on Apr 16, 2009 at 07:40 UTC ( [id://757877]=note: print w/replies, xml ) Need Help??


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

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{ $_ }}" };
  • Comment on Re^3: duplicates getting omitted while comparing values inside foreach.
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-26 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found