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: <%-{-{-{-<
In reply to Re: Code clarification - use of map and $$_
by AnomalousMonk
in thread Code clarification - use of map and $$_
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |