]>
Commit | Line | Data |
---|---|---|
7683e9e5 LT |
1 | #!/usr/bin/perl -w |
2 | ||
3 | use strict; | |
4 | ||
61f74164 | 5 | my %hash; |
7683e9e5 | 6 | |
61f74164 | 7 | # sort comparison functions |
7683e9e5 LT |
8 | sub by_category($$) { |
9 | my ($a, $b) = @_; | |
10 | ||
11 | $a = uc $a; | |
12 | $b = uc $b; | |
13 | ||
14 | # This always sorts last | |
15 | $a =~ s/THE REST/ZZZZZZ/g; | |
16 | $b =~ s/THE REST/ZZZZZZ/g; | |
17 | ||
61f74164 JP |
18 | return $a cmp $b; |
19 | } | |
20 | ||
21 | sub by_pattern($$) { | |
22 | my ($a, $b) = @_; | |
23 | my $preferred_order = 'MRPLSWTQBCFXNK'; | |
24 | ||
25 | my $a1 = uc(substr($a, 0, 1)); | |
26 | my $b1 = uc(substr($b, 0, 1)); | |
27 | ||
28 | my $a_index = index($preferred_order, $a1); | |
29 | my $b_index = index($preferred_order, $b1); | |
30 | ||
31 | $a_index = 1000 if ($a_index == -1); | |
32 | $b_index = 1000 if ($b_index == -1); | |
33 | ||
34 | if (($a1 =~ /^F$/ && $b1 =~ /^F$/) || | |
35 | ($a1 =~ /^X$/ && $b1 =~ /^X$/)) { | |
36 | return $a cmp $b; | |
37 | } | |
38 | ||
39 | if ($a_index < $b_index) { | |
40 | return -1; | |
41 | } elsif ($a_index == $b_index) { | |
42 | return 0; | |
43 | } else { | |
44 | return 1; | |
45 | } | |
7683e9e5 LT |
46 | } |
47 | ||
48 | sub alpha_output { | |
61f74164 JP |
49 | foreach my $key (sort by_category keys %hash) { |
50 | if ($key eq " ") { | |
51 | chomp $hash{$key}; | |
52 | print $hash{$key}; | |
53 | } else { | |
54 | print "\n" . $key . "\n"; | |
55 | foreach my $pattern (sort by_pattern split('\n', $hash{$key})) { | |
56 | print($pattern . "\n"); | |
57 | } | |
58 | } | |
7683e9e5 LT |
59 | } |
60 | } | |
61 | ||
62 | sub trim { | |
63 | my $s = shift; | |
64 | $s =~ s/\s+$//; | |
65 | $s =~ s/^\s+//; | |
66 | return $s; | |
67 | } | |
68 | ||
69 | sub file_input { | |
70 | my $lastline = ""; | |
71 | my $case = " "; | |
61f74164 | 72 | $hash{$case} = ""; |
7683e9e5 LT |
73 | |
74 | while (<>) { | |
75 | my $line = $_; | |
76 | ||
77 | # Pattern line? | |
78 | if ($line =~ m/^([A-Z]):\s*(.*)/) { | |
79 | $line = $1 . ":\t" . trim($2) . "\n"; | |
80 | if ($lastline eq "") { | |
61f74164 | 81 | $hash{$case} = $hash{$case} . $line; |
7683e9e5 LT |
82 | next; |
83 | } | |
84 | $case = trim($lastline); | |
61f74164 JP |
85 | exists $hash{$case} and die "Header '$case' already exists"; |
86 | $hash{$case} = $line; | |
7683e9e5 LT |
87 | $lastline = ""; |
88 | next; | |
89 | } | |
90 | ||
91 | if ($case eq " ") { | |
61f74164 | 92 | $hash{$case} = $hash{$case} . $lastline; |
7683e9e5 LT |
93 | $lastline = $line; |
94 | next; | |
95 | } | |
96 | trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'"); | |
97 | $lastline = $line; | |
98 | } | |
61f74164 | 99 | $hash{$case} = $hash{$case} . $lastline; |
7683e9e5 LT |
100 | } |
101 | ||
61f74164 JP |
102 | file_input(); |
103 | alpha_output(); | |
104 | ||
7683e9e5 | 105 | exit(0); |