Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

colomon (8994)

  (email not shown publicly)

Journal of colomon (8994)

Saturday December 27, 2008
09:03 AM

Third Script: Second Draft

[ #38169 ]
After a lot of really helpful suggestions from Aristotle, here is my second version of the third script. This version feels much more satisfyingly Perl6ish to me.

my $votes_file = "votes.txt";

sub EffectiveVote ( Str $vote, Hash %skip ) {
    return $vote.split( ',' ).first( { ! %skip{$_}.defined } )
        // die "No valid vote?!";

sub CountRound (Array @votes, Hash %skip)
    my %count;
    for @votes -> $vote
        %count{EffectiveVote($vote, %skip)}++;
    return %count;

my @votes = do
    my $votes = open($votes_file);

my $count = 0;
my %skip;

    say "\nRound {++$count}";
    my @ranking = CountRound(@votes, %skip).pairs.sort({.value}).reverse;

    say sprintf("%s: %s", .key, .value / @votes.elems)
        for @ranking;

    my $top_rank = @ranking[0];
    if ($top_rank.value > @votes.elems / 2)
        say "\nThe winner is {$top_rank.key} with {$top_rank.value / @votes.elems * 100.0}% of the vote.";

    my $skip = @ranking.pop.key;
    %skip{$skip} = 1;
    say "Dropping $skip";

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.