Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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]
[Discipulus]: no fear: moonday i'll back (unless a big quake ... ;=)
[Corion]: Discipulus: Being out in nature is great, at least as long as you have a dry place to sleep and a warm fire ;)
[Discipulus]: my whishlist include more "elements" anyway

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (12)
As of 2017-02-24 09:20 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (353 votes). Check out past polls.