Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Foreach loop explanation

by Kenosis (Priest)
on Nov 24, 2012 at 01:54 UTC ( #1005327=note: print w/replies, xml ) Need Help??

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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (13)
As of 2016-10-25 13:48 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (320 votes). Check out past polls.