Last active
August 29, 2015 14:22
-
-
Save davetang/84e9fc9c2b574c8aae3c to your computer and use it in GitHub Desktop.
Script that compares two files of IDs, that are on separate lines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# | |
# Script that compares two files of IDs, that are on separate lines | |
# See http://stackoverflow.com/questions/2933347/comparing-two-arrays-using-perl | |
# | |
use strict; | |
use warnings; | |
use Array::Utils qw(:all); | |
my $usage = "Usage: <infile.txt> <infile2.txt>\n"; | |
my $infile = shift or die $usage; | |
my $infile2 = shift or die $usage; | |
my @a = read_into_array($infile); | |
my @b = read_into_array($infile2); | |
# intersect | |
my @isect = intersect(@a, @b); | |
print "These ", scalar(@isect), " items/s are in both:\n\n@isect\n\n"; | |
# symmetric difference | |
my @diff = array_diff(@a, @b); | |
if (scalar(@diff) == 0){ | |
print "The lists are the same\n"; | |
} else { | |
print "These ", scalar(@diff), " item/s are different:\n\n@diff\n\n"; | |
my @a_not_b = array_minus(@a, @b); | |
my @b_not_a = array_minus(@b, @a); | |
if (scalar(@a_not_b) > 0){ | |
print "These ", scalar(@a_not_b), " item/s are in $infile but not in $infile2:\n\n@a_not_b\n\n"; | |
} | |
if (scalar(@b_not_a) > 0){ | |
print "These ", scalar(@b_not_a), " item/s are in $infile2 but not in $infile:\n\n@b_not_a\n\n"; | |
} | |
} | |
sub read_into_array { | |
my ($infile) = @_; | |
my @a = (); | |
open(IN,'<',$infile) || die "Could not open $infile: $!\n"; | |
while(<IN>){ | |
chomp; | |
push(@a, $_); | |
} | |
close(IN); | |
return(@a); | |
} | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment