Skip to content

Instantly share code, notes, and snippets.

@kidd
Created January 31, 2010 20:20
#!/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