-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection_of_two_lists.pl
72 lines (57 loc) · 1.66 KB
/
intersection_of_two_lists.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env perl
# Prints intersection of two lists.
# Usage:
# perl intersection_of_two_lists.pl [file with first list, one item per line]
# [file with second list, one item per line]
# Prints to console unless specified to print each heterozygosity table separately.
# To print to file, use
# perl intersection_of_two_lists.pl [file with first list, one item per line]
# [file with second list, one item per line] > [file with intersection]
my $list1_file = $ARGV[0]; # file with first list, one item per line
my $list2_file = $ARGV[1]; # file with second list, one item per line
my $NEWLINE = "\n";
# verifies that input files are provided, exist, and are not empty
if(!$list1_file or !-e $list1_file or -z $list1_file)
{
print STDERR "Error: first list not provided, does not exist, or is empty. "
."Exiting.\n";
die;
}
if(!$list2_file or !-e $list2_file or -z $list2_file)
{
print STDERR "Error: second list not provided, does not exist, or is empty. "
."Exiting.\n";
die;
}
# reads in the two lists
my %list1 = (); # key: value in first list -> value: 1
open LIST1, "<$list1_file" || die "Could not open $list1_file to read; terminating =(\n";
while(<LIST1>) # for each line in the file
{
chomp;
if($_ =~ /\S/)
{
$list1{$_} = 1;
}
}
close LIST1;
my %list2 = (); # key: value in second list -> value: 1
open LIST2, "<$list2_file" || die "Could not open $list2_file to read; terminating =(\n";
while(<LIST2>) # for each line in the file
{
chomp;
if($_ =~ /\S/)
{
$list2{$_} = 1;
}
}
close LIST2;
# identifies and prints values appearing in both lists
foreach my $value(sort keys %list1)
{
if($list2{$value})
{
print $value.$NEWLINE;
}
}
# January 27, 2025