chiark / gitweb /
compute-scottish-stv: --sort=alpha
[appendix-a6.git] / compute-scottish-stv
index 47604073388b5a14acc81a847ea9142e2ca05a2b..81dc6adeafb10d553c50b2153a250a530bfc8966 100755 (executable)
@@ -58,6 +58,8 @@ $SIG{__DIE__} = sub {
     die $_[0];
 };
 
+sub total_history_cmp ();
+
 sub unkopt ($$) {
     my ($what,$opt) = @_;
     if ($opt =~ m/^[A-Z]/) {
@@ -67,6 +69,19 @@ sub unkopt ($$) {
     }
 }
 
+my $display_cmp = \&total_history_cmp;
+
+while (@ARGV && $ARGV[0] =~ m/^\-/) {
+    $_ = shift @ARGV;
+    if (m/^--$/) {
+       last;
+    } elsif (m/^--sort=alpha$/) {
+       $display_cmp = sub { $b->{Cand} cmp $a->{Cand} };
+    } else {
+       die;
+    }
+}
+
 for (;;) {
     $_ = <>;
     if (m/^\| /) {
@@ -198,7 +213,7 @@ sub countballots () {
        $c->{History}[$stage-1] = $c->{Total};
     }
 
-    foreach my $c (reverse sort total_history_cmp
+    foreach my $c (reverse sort $display_cmp
                   grep { !$_->{NonCont} } values %cands) {
        prf "candidate %-10s: %s votes\n", $c->{Cand}, sv $c->{Total};
     }