http://www.perlmonks.org?node_id=1005327


in reply to Foreach loop explanation

In case you may be interested, List::Compare provides a means of getting the intersection of two lists. In addition to it being well designed, it will only return a list of unique elements--in case either list has repeating elements.

For example, modify Don Coyote's data to the following:

file1 ----- one two three four five six seven eight file2 ----- five five five six seven eight nine ten eleven twelve

And run the following on it:

#!/usr/bin/perl -w use strict; use warnings; use List::Compare; @ARGV == 2 or die 'Error: Two files needed for comparison.'; open my $inA, '<', $ARGV[0] or die qq{Unable to open file "$ARGV[0]": +$!}; chomp( my @nomi = <$inA> ); close $inA; open my $inB, '<', $ARGV[1] or die qq{Unable to open file "$ARGV[1]": +$!}; chomp( my @indirizzi = <$inB> ); close $inB; my $lc = List::Compare->new(\@nomi, \@indirizzi); my @intersection = $lc->get_intersection; print "$_\n" for @intersection;

Here's the output:

eight five seven six