I've inherited a set of scripts and, not being an expert in Perl, there's a particular chunk of code which i'm struggling to understand:
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"; } } }
This takes an input file in this format:
1 111 C T 1 0 6 1 136 G A 1 0 6 1 136 G A 1 0 9 1 244 C CT 1 0 2 1 262 A G 1 0 2 1 268 A C 1 0 2 1 268 A C 1 0 4 1 270 C T 1 0 2
Finds all unique entries (based on the first four columns), averages the 5th and sums the 6th.
There's a few functions and pieces of syntax i'm unfamiliar with. Specifically, the use "$$_" (the use of a double $), exactly why 'map' is used here and how this line:
map {$x += $$_[0]; $y += $$_[1];} @{$r{$k};};
Is producing the desired result in general. If anybody could explain or clarify how this chunk of code works, that would be great, thanks!
In reply to Code clarification - use of map and $$_ by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |