venkates wrote:
Hi all,

Hello,

I have written a piece of code which takes in to list of terms and
retrieves the intersection between the two list of terms. The code works
fine but it the intersection list has redundant in its terms. How do I
fix this?

perldoc -q intersection


The code is given below.

#!/usr/bin/perl -w

use Carp;
use strict;
use warnings;

# Takes in first argument
my ($input_file1, $input_file2 ) = @_;

That makes no sense. @_ only contains data from inside a subroutine. Perhaps you meant:

my ($input_file1, $input_file2 ) = @ARGV;


# Opens the input files
open my $fh, "<", $ARGV[0] or die "could not open $ARGV[0]: $!";
my @list1 = <$fh>;


open my $fh1, "<", $ARGV[1] or die "could not open $ARGV[1]: $!";
my @list2 = <$fh1>;

# generates an intersection of terms from the first and second arguments
my @intersection_terms;
foreach (@list1)
{
my $itemlist1 = $_;

Why not just:

foreach my $itemlist1 (@list1)

Why assign the same value to two different variables, one of which you are not even using?


foreach (@list2)
{
my $itemlist2 = $_;

Again, why not just:

foreach my $itemlist2 (@list2)


if ($itemlist1 eq $itemlist2)
{
push @intersection_terms, $itemlist2;
}
}
}
print @intersection_terms, "\n";


exit 0;



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to