perllearner007 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -w use strict; #Find intersection that are the commom genes in both the list open (FIRST, "C:/Users/ABC/Desktop/list2.txt") or die; open (SECOND, "C:/Users/ABC/Desktop/list1.txt") or die; my @first = (<FIRST>); chomp (@first); my @second = (<SECOND>); chomp (@second); my @union = my @isect = my @sym_diff = (); my %union = my %isect = my %count = (); foreach my $e (@first, @second) { $count{$e}++; } foreach my $e (keys %count) { push(@union, $e); if ($count{$e} == 2) { push @isect, $e; } else { push @sym_diff, $e; } } my %seen; my @first_only; @seen{@second} = (); foreach my $item (@first) { push (@first_only, $item) unless exists $seen{$item}; } @isect = sort (@isect); print "Intersection: " . scalar(@isect) . " " . join (" ", @isect) . " +\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Common between two lists
by choroba (Cardinal) on Dec 09, 2011 at 00:57 UTC | |
|
Re: Common between two lists
by umasuresh (Hermit) on Dec 09, 2011 at 15:34 UTC | |
by aaron_baugher (Curate) on Dec 09, 2011 at 16:35 UTC |