There is much good advice and learning throughout the thread... to sum up what my recommendations would be
- add comments as you learn things, so when you go to support it a year down the road, and have forgotten everything you learned in this thread, you'll at least have comments to guide you. (also, add a comment to link to this thread. :-) )
- Re: Code clarification - use of map and $$_ = switch from map to a for or foreach loop; additionally, I'd recommend using a meaningful loop variable name (I'd call it $pair_ref or similar, to indicate it's a reference to a pair of something) rather than the default $_
- instead of using the $$_[0] notation, or even the more obviously meaningful $_->[0], for accessing the elements of the pair, I'd probably change to assigning the individual elements to meaningful variables:
foreach my $pair_ref ( @{ $r{$k} } ) { # for each [col4,col6] pai
+r that matched on the four-column key
my ($col4, $col6) = @$pair_ref; # get the (col4,col6) valu
+es
$x += $col4; # x is the summation of al
+l the matching col4 values
$y += $col6; # similar for y
}
- Re: Code clarification - use of map and $$_ = move the END block out of the while(<IN>) loop
- Fix the print statement in the END block to not use @g[1], since that's a 1-element slice, and is better written as $g[1]. You might want to further look into the perlvar $, and $" variables for automatically joining within your print statement (or use a manual join function if you want to make the joining explicit1) rather than manually placing tabs between each element of the @g array, $x/..., and $y values.
1 I know for many non-expert Perl coders, the explicit join is more natural and possibly easier to remember in the future than the magic variables; personally, despite having hacked Perl for ... eek, two decades now! -- it wasn't until I started really frequenting perlmonks a few months back that I actually understood what the $, and $" variables do, and started using them