From: Ian Jackson Date: Sun, 21 Aug 2016 11:19:32 +0000 (+0100) Subject: spi2loose and normalise fixes X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=appendix-a6.git;a=commitdiff_plain;h=7b6aa25ec2857da33d21b3f823f9301c7fb1cee3;ds=inline spi2loose and normalise fixes --- diff --git a/README.format b/README.format index 5ec7903..6bbc5d6 100644 --- a/README.format +++ b/README.format @@ -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. diff --git a/normalise b/normalise index 6bf8ea6..7db0740 100755 --- 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 $!; diff --git a/spi2loose b/spi2loose index 9965502..89a6a67 100755 --- 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 $!; }