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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.