[Wellington-pm] ? shorten an Perl snippet
Daniel Pittman
daniel at rimspace.net
Mon Aug 2 17:16:54 PDT 2010
Dan Horne <dan.horne at redbone.co.nz> writes:
> Without wanting to golf it, perhaps something like
>
> my $product = 0;
> for my $j (10 .. 59) {
> $product += $split_line[$j][5];
> }
> $product = $product/50;
If split_line was a flat array, not a nested array, this would help:
use List::Util qw{sum};
my $product = sum(@split_line[10..59]) / 50;
It is roughly the same as the open-coded loop above, but IMO using the 'sum'
function is a bit clearer for the reader.
For nested arrays like this example, though, there isn't a neater way other,
maybe, than open-coding the extraction with map and all. (That I can think of.)
Daniel
--
✣ Daniel Pittman ✉ daniel at rimspace.net ☎ +61 401 155 707
♽ made with 100 percent post-consumer electrons
More information about the Wellington-pm
mailing list