chiark / gitweb /
spi2loose and normalise fixes
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Aug 2016 11:19:32 +0000 (12:19 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Aug 2016 11:19:32 +0000 (12:19 +0100)
README.format
normalise
spi2loose

index 5ec79035cb7e09eb84aa04dbc86b76c459c2a7a8..6bbc5d605146ae2eae18b7972ceb609e78661586 100644 (file)
@@ -38,6 +38,9 @@ cannot contain leading or trailing whitespace).
 
 VOTERNAMEs need not be distinct and may be empty.
 
+Ballots may contain zero or more preferences, each being one or more
+CAND joined by `='s.  Preferences are separated by spaces.
+
 OPTION is OPTNAME[=VALUE] and modifies the preference, ballot, or whole
 election.  VALUE may contain no whitespace.
 
index 6bf8ea664d7b54fce5b65f3c2faea20fe90a51d7..7db074006a9f6627376b7e88ea5276c0b859318f 100755 (executable)
--- a/normalise
+++ b/normalise
@@ -2,18 +2,26 @@
 
 use strict;
 
-our @options, %candiates, @ballots;
+our @options;
+our %candidates; # $candidates{CAND}{Desc}, {Opts}[]
+our @ballots;
 
 my $candvoter_re = '\w+';
 
+sub badinput ($) {
+    die "bad input: $_[0]";
+}
+
 sub normalise_opts_list ($) {
+    my ($os) = @_;
+    $os //= '';
     my @o;
     foreach my $o (split /\s+/, $os) {
        if ($o =~ m/^\w+$/) {
            push @o, $&;
        } elsif ($o =~ m/^\w+\=\S+$/) {
-           push @o, $&;S
-       } elseif ($o !~ m/\S/) {
+           push @o, $&;
+       } elsif ($o !~ m/\S/) {
        } else {
            badinput "bad option \`$o'";
        }
@@ -34,7 +42,9 @@ while (<>) {
     s/\s+$//;
     if (m/^\|/) {
        push @options, normalise_opts_list $';
-    } elsif (m/^($candvoter_re?)\s*=\s*([^|]+?)\s*|(.*)?$/) {
+    } elsif (m/^($candvoter_re?)\s*=\s*([^|]+?)\s*\|(.*)?$/o) {
+       use Data::Dumper;
+print STDERR Dumper($1,$2,$3);
        my ($cand,$desc,$opts) = ($1,$2,$3);
        push @{ $candidates{$cand}{Opts} }, normalise_opts $opts;
        if (length $desc) {
@@ -42,15 +52,15 @@ while (<>) {
                defined $candidates{$cand}{Desc};
            $candidates{$cand}{Desc} = $desc;
        }
-       $desc=$cand unless length $desc;
-       push @candidates, "$cand = $desc".
-    } elsif (m/^($candvoter_re?)?\s*\:([^|]+)(|(.*)?$/) {
+    } elsif (m/^($candvoter_re?)?\s*\:([^|]*)(?:\|(.*))?$/) {
        my ($voter,$opts) = ($1,$3);
        my @p;
        foreach my $p (split /\s+/, $2) {
            if ($p =~ m/^\w+(?:\=\w+)*$/) {
                push @p, $&;
-               $candidates{$_} //= { } foreach my $p =~ m/\w+/g;
+               $candidates{$_} //= { } foreach $p =~ m/\w+/g;
+           } elsif ($p eq '') {
+               # empty entry can only happen if voter casts no prefs at all
            } else {
                badinput "bad vote preference \`$p'";
            }
@@ -75,6 +85,6 @@ foreach my $cand (sort keys %candidates) {
 sub vsortkey { $_[0] =~ m/:/; return "$' : $`"; }
 
 print $_,"\n" or die $! foreach
-    (sort { vsortkey($a) cmp vsortkey($b) } @ballots;
+    sort { vsortkey($a) cmp vsortkey($b) } @ballots;
 
 print ".\n" or die $!;
index 996550277fb4023839cc68fbb0628d97898cc604..89a6a675d0f4378f58313200ac04263d051ce72e 100755 (executable)
--- a/spi2loose
+++ b/spi2loose
@@ -1,8 +1,8 @@
 #!/usr/bin/perl -w
 use strict;
 while (<>) {
-      m/^(\w+) (\w+)$/ or die "$_ ?";
-      print "$1 : ".(join " ", split //, $2)."\n" or die $!;
+      m/^(\w+)(?:\s+(\w+))?$/ or die "$_ ?";
+      print "$1 : ".(join " ", split //, ($2 // ''))."\n" or die $!;
 }