Skip to content

Instantly share code, notes, and snippets.

@davetang
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetang/84e9fc9c2b574c8aae3c to your computer and use it in GitHub Desktop.
Save davetang/84e9fc9c2b574c8aae3c to your computer and use it in GitHub Desktop.
Script that compares two files of IDs, that are on separate lines
#!/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