Skip to content

Instantly share code, notes, and snippets.

@kidd
Created January 31, 2010 20:21
Show Gist options
  • Select an option

  • Save kidd/291222 to your computer and use it in GitHub Desktop.

Select an option

Save kidd/291222 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use List::Util qw(sum);
sub foreachLine {
my ($const, $finalize) = @_;
while (<DATA>) {
$const->($_);
}
$finalize->();
}
{
#closure
my @keep;
sub gen_fun {
my $par = shift;
my $k = \@keep;
return sub {
my $student = shift; # gets a line like "Raimon Grau 6.4"
my ($qualif) = $student =~ m/ ([^ ]*)$/;
push @$k, $student if ($qualif > $par);
}
}
sub end_do {
print "@keep\n";
my @a = map{
my ($qualif) = m/ ([^ ]*)$/;
$qualif
} @keep;
print sum(@a)/@a , "\n";
}
}
my $a = gen_fun(shift);
foreachLine($a , \&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