chiark / gitweb /
compute-scottish-stv: format output for slightly easier comparison
[appendix-a6.git] / compute-scottish-stv
index 47604073388b5a14acc81a847ea9142e2ca05a2b..a7fb3780d96113ab1e8921d57a2e1d60d92d7688 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/^\| /) {
@@ -186,7 +201,7 @@ sub sv ($) {
 
 sub prf {
     my $fmt = shift;
-    printf "stage %d: ".$fmt, $stage, @_;
+    printf " ".$fmt, @_;
 }
 
 sub countballots () {
@@ -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};
     }
@@ -208,7 +223,8 @@ sub computequota () {
     my $totalvalid = 0/1;
     $totalvalid += $_->{Total} foreach values %cands;
     $quota = floor($totalvalid / (1 + $seats) + 1);
-    prf "total valid %s quota %s\n", (sv $totalvalid), (sv $quota);
+    prf "total valid %s\n", sv $totalvalid;
+    prf "quota       %s\n", sv $quota;
 }
 
 sub total_history_cmp () {
@@ -296,6 +312,7 @@ $stage = 0;
 
 for (;;) {
     $stage++;
+    printf "stage %3d:\n", $stage;
 
     sortballots @allvotes if $stage == 1;