foreach my $pair_ref ( @{ $r{$k} } ) { # for each [col4,col6] pair that matched on the four-column key my ($col4, $col6) = @$pair_ref; # get the (col4,col6) values $x += $col4; # x is the summation of all the matching col4 values $y += $col6; # similar for y }