-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateMBlists.pl
110 lines (83 loc) · 2.83 KB
/
updateMBlists.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env perl
# script to read sqlite output and calculate MB list deltas & fix duplicate emails
use Array::Utils qw(:all);
use Getopt::Std;
getopts('aD');
Usage() unless ($#ARGV == 0);
$input = $ARGV[0];
warn "Loading date from $input\n\n";
# these fields are required
@inputRequired = (
'id',
'Name',
'email',
'notes',
'district',
'mbold',
'mbnew'
);
open(INPUT, '<', $input) or die $!;
# id Name email notes district mbold mbnew
# 0 1 2 3 4 5 6
while (<INPUT>)
{
chomp;
next if (/^\s*$/); # skip blank lines
s/"//g; # remove redundant quotes from all fields
@f = split(/\t/);
if ($. == 1)
{
# map field names to offsets
$i = 0;
for $f (@f)
{
$inputFields{$f} = $i++;
}
die "Improper file format $_\nRequired: " . join(', ', @inputRequired) . "\n" unless (Contains(\@f, \@inputRequired));
print join(',', qw(id name email notes district mbdelta mbnew)) . "\n";
next;
}
# remove the '*' on the end of the new list elements
$f[$inputFields{'mbnew'}] =~ s/\s+\*//g;
$f[$inputFields{'mbnew'}] =~ s/Communication/Communications/ig;
$f[$inputFields{'mbnew'}] =~ s/Computers/Digital Technology/ig;
$f[$inputFields{'mbold'}] =~ s/Computers/Digital Technology/ig;
$f[$inputFields{'mbold'}] =~ s/\s*\(troop only\)//ig;
# force same case on both lists
$f[$inputFields{'mbnew'}] =~ s/(\w+)/ucfirst(lc($1))/eg;
$f[$inputFields{'mbold'}] =~ s/(\w+)/ucfirst(lc($1))/eg;
@mbold = sort(split(/\|/, $f[$inputFields{'mbold'}]));
@mbnew = sort(split(/\|/, $f[$inputFields{'mbnew'}]));
# https://metacpan.org/pod/Array::Utils
# get items from @a not in @b
# array_minus( @a, @b );
my @removeMBs = map {'-'.$_} array_minus( @mbold, @mbnew ); # old that are not in new
my @addMBs = map {'+'.$_} array_minus( @mbnew, @mbold ); # new that were not in old
# remove duplicate emails
my $emails = join(', ', unique(split(/\s*,\s*/, lc($f[$inputFields{'email'}]))));
print qq!"! . join(qq!","!,$f[$inputFields{'id'}], $f[$inputFields{'Name'}], $emails, $f[$inputFields{'notes'}],
$f[$inputFields{'district'}], join(', ', @addMBs, @removeMBs),
join(', ', @mbnew) ) . qq!"\n!;
}
close(INPUT);
# usage Contains (\@haystack, \@needle)
# think of it as @haystack contains @needle
sub Contains
{
my ($haystack, $needle) = @_;
my %haystackHash = map {$_ => 1} @{$haystack};
for (@{$needle})
{
return 0 if (!exists($haystackHash{$_}));
}
return 1;
}
sub Usage
{
die <<EOS;
Usage: $0 [-D] update.tsv
update.tsv = tab seperated list of people with old and new badges
options:
-D : turn on debug messages
EOS
}