sub pairwise_tally { my ($self, $votelist)=@_; # $self is an object (ala Perl 5) that stores all basic pairwise # election data # # @self->{'tally'} is a two-dimensional array (also ala Perl 5) that # stores the pairwise tally results. # # $self->{'tally'}[$candx][$candy] is the number of votes that $candx # received over $candy. # # $votelist is a string that is essentially a raw text file that # contains all of the ballots. for(split(/\n/,$votelist)) { # $loservec-a boolean vector with a flag set for all "losers" # reset with every new ballot. All are losers until they are # listed on a ballot. my($loservec)=$self->{'candvec'}; # Parse ballot. Skip if no ballot is returned. (!(my($ballot)=new ballot_obj($self, $_))) && next; # @{$ballot->{'rankings'}} is an array of integers # representing the candidates this voter (or voters) voted # for, in order of preference. # In addition, $ballot->{'quantity'} is the number of identical # ballots we are considering at this time. my(@votelist)=@{$ballot->{'rankings'}}; foreach $tier (@votelist) { # For each preference listed... # Remove the chosen candidate(s) from the loser vector. foreach $peer (@{$tier}) { vec($loservec, $peer, 1) = 0; } # For all candidates... for ($i = 0; $i<= $#{$self->{'candidate'}}; $i++) { # If said candidate hasn't been listed yet... if(vec($loservec,$i,1)) { # ...they've been beat by the chosen candidate. # Increment their "votes for the other guy" # counter by the appropriate number of ballots foreach $peer (@{$tier}) { if(defined($self->{'tally'}[$peer][$i])) { $self->{'tally'}[$peer][$i] +=$ballot->{'quantity'}; } else { $self->{'tally'}[$peer][$i] =$ballot->{'quantity'}; } } } } } $self->{'total_vote'}+=$ballot->{'quantity'}; } }