blob: 5c8e0504c67e406386da763df2268adf3c57aef2 (
plain)
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
|
#!/usr/bin/perl -w
use strict;
my %hash;
# sort comparison functions
sub by_category($$) {
my ($a, $b) = @_;
$a = uc $a;
$b = uc $b;
# This always sorts last
$a =~ s/THE REST/ZZZZZZ/g;
$b =~ s/THE REST/ZZZZZZ/g;
return $a cmp $b;
}
sub by_pattern($$) {
my ($a, $b) = @_;
my $preferred_order = 'MRPLSWTQBCFXNK';
my $a1 = uc(substr($a, 0, 1));
my $b1 = uc(substr($b, 0, 1));
my $a_index = index($preferred_order, $a1);
my $b_index = index($preferred_order, $b1);
$a_index = 1000 if ($a_index == -1);
$b_index = 1000 if ($b_index == -1);
if (($a1 =~ /^F$/ && $b1 =~ /^F$/) ||
($a1 =~ /^X$/ && $b1 =~ /^X$/)) {
return $a cmp $b;
}
if ($a_index < $b_index) {
return -1;
} elsif ($a_index == $b_index) {
return 0;
} else {
return 1;
}
}
sub alpha_output {
foreach my $key (sort by_category keys %hash) {
if ($key eq " ") {
chomp $hash{$key};
print $hash{$key};
} else {
print "\n" . $key . "\n";
foreach my $pattern (sort by_pattern split('\n', $hash{$key})) {
print($pattern . "\n");
}
}
}
}
sub trim {
my $s = shift;
$s =~ s/\s+$//;
$s =~ s/^\s+//;
return $s;
}
sub file_input {
my $lastline = "";
my $case = " ";
$hash{$case} = "";
while (<>) {
my $line = $_;
# Pattern line?
if ($line =~ m/^([A-Z]):\s*(.*)/) {
$line = $1 . ":\t" . trim($2) . "\n";
if ($lastline eq "") {
$hash{$case} = $hash{$case} . $line;
next;
}
$case = trim($lastline);
exists $hash{$case} and die "Header '$case' already exists";
$hash{$case} = $line;
$lastline = "";
next;
}
if ($case eq " ") {
$hash{$case} = $hash{$case} . $lastline;
$lastline = $line;
next;
}
trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'");
$lastline = $line;
}
$hash{$case} = $hash{$case} . $lastline;
}
file_input();
alpha_output();
exit(0);
|