#!/opt/local/bin/perl # quick and dirty alternative software to convert FormReader CSV output # to ChoicePlus Pro input. To use redirect output to .chp file. # -bks 27 november 2007 use constant WRITEIN_ID => 1; if ($#ARGV == 0) { my $csvFile = $ARGV[0]; open(CSV, "<$csvFile") or die "Cannot open $csvFile"; } else { die "Usage: csv2chp csvfile"; } # boiler plate for delegate elections print ".TITLE Delegate Election\n"; print ".CONTEST DelegateElectionTransfer\n"; print ".TIES BY-HUMAN RANDOM\n"; print ".ELECT 9\n\n\n"; # first line of CSV lists candidates. "write-in" must be first candidate. my $line = ; chomp $line; @candidates = split(/,/, $line); $numcandidates = @candidates; # emit .CANDIDATE lines my $i = 1; while (my $c = shift(@candidates)) { printf ".CANDIDATE %d, \"%s\"\n", $i++, $c; } print "\n\n\n"; # compute and emit ballots my $ballotID = 1; while ($line = ) { chomp $line; @rankings = split(/,/, $line); @prefs = (); if ($rankings[0] == WRITEIN_ID) { # in 2007, the single write-in could only be ranked first push @{ $prefs[$rankings[0]]}, WRITEIN_ID; } foreach my $i (1..$numcandidates + 1) { # cell content is ranking, or -1 if not ranked if ($rankings[$i] > 0 && $rankings[$i] <= $numcandidates) { # candidate ID is index in CSV vector, plus one push @{$prefs[$rankings[$i]]}, $i + 1; } } print "$ballotID) "; $, = "="; my $i = 0; do { $i++; } until $i > $#prefs || @{$prefs[$i]}; print @{$prefs[$i]}; while (++$i && $i <= $#prefs) { if (@{$prefs[$i]}) { print ","; print @{$prefs[$i]}; } } print "\n"; $ballotID++; } close(CSV);