in reply to take 'n' array elements at a time
A late TMTOWTDI , perhaps not rock-solid:
#!/usr/bin/env perl use strict; use warnings; use List::MoreUtils qw(natatime); use List::Util qw(reduce); use IO::All; use autodie; my $file = shift; my $amount = 2; my $io = io $file; my $iterator = natatime $amount, @$io; while ( my @items = $iterator->() ) { my @values = map { /^.+\|(.+)$/ } @items; my $sum = reduce { $a + $b } @values; my $average = $sum / $amount; print qq($average\n); } __END__
Update: I took the title of your OP ("...take 'n' at a time...") literally. Or i tried it, at least ;-)
Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
|
|---|