Algorithm::Voting::Plurality - use "Plurality" to decide the sole winner
# construct a "ballot box" use Algorithm::Voting::Ballot; use Algorithm::Voting::Plurality; my $box = Algorithm::Voting::Plurality->new(); # add ballots to the box $box->add( Algorithm::Voting::Ballot->new('Ralph') ); $box->add( Algorithm::Voting::Ballot->new('Fred') ); # ... $box->add( Algorithm::Voting::Ballot->new('Ralph') ); # and print the result print $box->as_string;
From http://en.wikipedia.org/wiki/Plurality_voting_system:
The plurality voting system is a single-winner voting system often used to elect executive officers or to elect members of a legislative assembly which is based on single-member constituencies.
The most common system, used in Canada, India, the UK, and the USA, is simple plurality, first past the post or winner-takes-all, a voting system in which a single winner is chosen in a given constituency by having more votes than any other individual representative.
And from http://en.wikipedia.org/wiki/Plurality:
In voting, a plurality vote is the largest number of votes to be given any candidate or proposition when three or more choices are possible. The candidate or proposition receiving the largest number of votes has a plurality. The concept of "plurality" in voting can be contrasted with the concept of "majority". Majority is "more than half". Combining these two concepts in a sentence makes it clearer, "A plurality of votes is a total vote received by a candidate greater than that received by any opponent but less than a majority of the vote."
Constructs a "ballot box" object that will use the Plurality criterion to decide the winner. Optionally, specify a list of candidates; any ballot added to the box that does not indicate one of the listed candidates throws an exception.
Example:
# construct a ballot box that accepts only three candidates my @c = qw( John Barack Ralph ); my $box = Algorithm::Voting::Plurality->new(candidates => \@c);
Returns a list containing the candidate names used in the construction of the ballot box. If no candidates were specified at construction of the box, the empty list is returned.
Add $ballot to the box. $ballot can be any object that we can call method candidate() on.
$ballot
candidate()
Increments the tally for $candidate by 1.
$candidate
If this election is limited to a specific list of candidates, this method will die() if the candidate on $ballot is not one of them.
die()
Returns the total number of ballots cast so far.
The result is a "digested" version of the ballot tally, ordered by the number of ballots cast for a candidate.
This method returns a list of arrayrefs, each of the form [$n, @candidates], and sorted by decreasing $n. Candidates "tied" with the same number of votes are lumped together.
[$n, @candidates]
$n
For example, an election with three candidates A, B, and C, getting 100, 200, and 100 votes respectively, would generate a result structure like this:
[ [ 200, "B" ], [ 100, "A", "C" ], ]
Returns a string containing the election results.
To install Algorithm::Voting, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Algorithm::Voting
CPAN shell
perl -MCPAN -e shell install Algorithm::Voting
For more information on module installation, please visit the detailed CPAN module installation guide.