in reply to Code clarification - use of map and $$_
while (<IN>) { our(@F) = split(/\s+/, $_, 0); push @{$r{join ' ' x 8, @F[0..3]};}, [@F[4, 6]]; sub END { foreach $k (keys %r) { my($x, $y); map {$x += $$_[0]; $y += $$_[1];} @{$r{$k};}; my @g = split(/\s+/,$k); print OUT "$g[0]\t@g[1]\t@g[2]\t@g[3]\t", $x / scalar(@{$r +{$k};}), "\t$y\n"; } } }
Another odd thing to note about this code is the END block planted in the middle of it, written in a disparaged way as a sub block. Please see the "BEGIN, UNITCHECK, CHECK, INIT and END" section in perlmod. Because all END blocks run at the end (!) of all other code, I think this chunk of code could more clearly and conventionally be written as:
Good luck.while (<IN>) { our(@F) = split(/\s+/, $_, 0); push @{$r{join ' ' x 8, @F[0..3]};}, [@F[4, 6]]; } ... all other code ... END { foreach $k (keys %r) { my($x, $y); map {$x += $$_[0]; $y += $$_[1];} @{$r{$k};}; my @g = split(/\s+/,$k); print OUT "$g[0]\t@g[1]\t@g[2]\t@g[3]\t", $x / scalar(@{$r{$k} +;}), "\t$y\n"; } }
Give a man a fish: <%-{-{-{-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Code clarification - use of map and $$_
by pryrt (Abbot) on Aug 09, 2016 at 15:16 UTC | |
by AnomalousMonk (Archbishop) on Aug 09, 2016 at 15:28 UTC | |
by pryrt (Abbot) on Aug 09, 2016 at 15:21 UTC |