#! /usr/bin/perl -w #Written by CSR on 11.7.08 #Input: list1 and list2 (two files). #Output: union, intersection, unique to list1, and unique to list2. use strict; use List::Compare; use Getopt::Std; #command line arguments: import list1 and list2 using "-a" and "-b". my $usage = "Warning, missing information!\nUsage is: ./two_list_comparison1.0.pl -a [list1 filename] -b [list2 filename]\n"; my %args; getopts('a:b:', \%args); my $file1; my $file2; if ($args{a}) { $file1 = $args{a}; } else { die "$usage"; } if ($args{b}) { $file2 = $args{b}; } else { die "$usage"; } #one array for each list my @list1 = (); my @list2 = (); #generate an array of items from list1 open (LIST1 , $file1) or die "COULD NOT OPEN FILE"; while (my $line1 = ) { chomp $line1; push @list1 , $line1; } close (LIST1); #generate an array of items from list2 open (LIST2 , $file2) or die "COULD NOT OPEN FILE"; while (my $line2 = ) { chomp $line2; push @list2 , $line2; } close (LIST2); my $lc = List::Compare->new(\@list1, \@list2); #get the union my @union = $lc->get_union; #create a filename and print the union list to a file. my $unionfile = $file1."_and_".$file2.".union"; open (UNION_FH , "> $unionfile") or die "COULD NOT OPEN UNION FILE"; foreach my $union(@union) { print UNION_FH "$union\n"; } close(UNION_FH); #get the intersection my @intersection = $lc->get_intersection; #create a filename and print the intersection list to a file. my $intersectionfile = $file1."_and_".$file2.".intersection"; open (INTERSECTION_FH , "> $intersectionfile") or die "COULD NOT INTERSECTION OPEN FILE"; foreach my $intersection(@intersection) { print INTERSECTION_FH "$intersection\n"; } close (INTERSECTION_FH); #get the items unique to list1 my @Lonly = $lc->get_Lonly; #create a filename and print the unique2list1 to a file. my $Lonlyfile = $file1."_and_".$file2.".unique_to_$file1"; open (Lonly_FH , "> $Lonlyfile") or die "COULD NOT OPEN Lonly FILE"; foreach my $Lonly(@Lonly) { print Lonly_FH "$Lonly\n"; } close (Lonly_FH); #get the items unique to list2 my @Ronly = $lc->get_Ronly; #create a filename and print the unique2list2 to a file. my $Ronlyfile = $file1."_and_".$file2.".unique_to_$file2"; open (Ronly_FH , "> $Ronlyfile") or die "COULD NOT OPEN Ronly FILE"; foreach my $Ronly(@Ronly) { print Ronly_FH "$Ronly\n"; } close (Ronly_FH);