Stories
Slash Boxes
Comments
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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • sub EffectiveVote ( Str $vote, Hash %skip ) {
        return $vote.split( ',' ).first( { ! %skip{$_}.defined } );
        die "No valid vote?!";
    }

    • Okay, that's definitely a big improvement over mine, and quite elegant. However, I'm wondering about return versus die here. (And my wondering is more complicated because I cannot find documentation for .first anywhere.)

      If no valid vote is found, won't the first line return undefined rather than not return?

      • Argh, yes. Good point.

        • Would

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

          do it, in theory? (In practice, I haven't gotten err die to work for me yet.)

          • Hey,

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

            actually compiled and it works! (I tested created a second votes file to make sure that it properly caught the condition, and it does.)

  • sub CountRound ( @votes, %skip ) {
        my %fraction;
        my $f = { EffectiveVote( $^vote, %skip ) };
        for @votes.map( $f ) { %fraction{$_}++ }
        for %fraction.values { $_ /= @votes.elems }
        return %fraction;
    }

    • That's an interesting approach. On consideration, I decided it would be easier to drop the percentage thing altogether, yielding this, which is slightly wordier than yours but dead simple.

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

  • Unlike the others this is untested.

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

    my $count = 0;
    my %skip;

    loop {
        say "\nRound {++$count}";

        my @ranking = CountRound( @votes, %skip ).pairs.sort({.value});

        say sprintf "%s: %s", .key, .value
            for @ranking;

        given @ranking[0].value {
            when $_ > 0.5 {
                say sprintf "\nThe winner i

    • That CountRound line is nice. I've worked it into my code, I'll post another full version as a fresh post. Thanks!