Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use strict;
- use warnings;
- use List::Util qw(sum);
- # An example for closures in Perl,
- # Also an example for two competing implementations of a function
- # Here: the sum of all numbers of a column in an Array of Arrays
- my @test = (
- [ "asd", "edsfsdfs", "345345545", "dasdas", "1,234.5" ],
- [ "asd", "edsfsdfs", "345345545", "dasdas", "1,234.5" ],
- );
- my $sum4 = colsum(4);
- my $_sum4 = _colsum(4);
- print $sum4->( \@test )."\n";
- print $_sum4->( \@test )."\n";
- sub colsum {
- my $idx = shift;
- return sub {
- my $result = 0;
- my @a = @{ scalar shift };
- foreach my $x (@a) {
- my $y = $x->[$idx];
- $y =~ s/,//g;
- $result += 1*$y;
- }
- return $result;
- }
- }
- sub _colsum {
- my $idx = shift;
- return sub {
- return sum map { $_->[$idx] =~ s/,//gr } @{ scalar shift };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement