Created
January 31, 2010 20:20
-
-
Save kidd/291221 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use strict; | |
| use warnings; | |
| use List::Util qw(sum); | |
| my $par = shift; | |
| sub foreachLine { | |
| my ($const, $finalize) = @_; | |
| while (<DATA>) { | |
| $const->($_); | |
| } | |
| $finalize->(); | |
| } | |
| { | |
| #closure | |
| my @keep; | |
| sub constrain { | |
| my $student = shift; # gets a line like "Raimon Grau 6.4" | |
| my ($qualif) = $student =~ m/ ([^ ]*)$/; | |
| push @keep, $student if ($qualif > $par); | |
| } | |
| sub end_do { | |
| my @a = map{ | |
| my ($qualif) = m/ ([^ ]*)$/; | |
| $qualif | |
| } @keep; | |
| print sum(@a)/@a , "\n"; | |
| } | |
| } | |
| foreachLine(\&constrain , \&end_do); | |
| __DATA__ | |
| foo 6.3 | |
| bar 4.3 | |
| baz 8.3 | |
| quux 8.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment