That's got some strange notation -- lots of semicolons where they are technically allowed, but I've never seen anybody use them there. And trying to make lots of subs called END, one per line of the input file, is just unfathomable. (I tried a quick test where I tried to make multiple named subs inside a loop like that, and call them both inside and outside the loop; it didn't do anything that makes sense to me.) Also, that sub END is never called, at least in your snippet.

However, when I just removed sub END from before that block, so that it would just execute the block, and set *OUT = *STDOUT, I was able to better see what was going on. To help figure things out, I also added some print statements before and inside map's block

push @{$r{join ' ' x 8, @F[0..3]};}, [@F[4, 6]]; { foreach my $k (keys %r) { my($x, $y); print "\$r{\$k};", $r{$k}; print "\@{\$r{\$k};}", @{$r{$k};}; map {$x += $$_[0]; $y += $$_[1]; print "\$_='$_'", "SS_[0]=$$_[0]", "SS_[1]=$$_[1]", "x=$x", "y=$y"; } @{$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"; } }

From what I can tell, you've got a hash %r, with keys made from the joining the first four columns. Each element of that hash is an array ref; the array contained within holds array-refs to the col4,col6 pairs. Thus, the map line says: For a given key $k, get the array behind the array ref for that element (@{ $r{$k} }). The map says, for each element in that array (so, for each array ref that points to the col4,col6 pairs), which map's block will refer to as $_, run the block. The block says to add the col4 value (which is the first element of the array referenced by $_) to $x and add the col6 value (which is the second element of the array referenced by $_) to $y.

That's as confusing as mud, I'm sure. When $k refers to the second '1 136 G A' line:

$k; # == "1 136 G A" with more spaces $r{$k}; # == [ [1,6], [1,9] ] == referenece to an array of arr +ay-refs @{$r{$k}}; # == ( [1,6], [1,9] ) == array of array-refs map {} @ # for each element in the @ array, run the {} block # First element of @ is the first ref to a pair-array: $_; # = [1,6] == array ref $$_; # = (1,6) == array $$_[0]; # = 1 == first element of array (1,6) or arrayref [1,6] $$_[1]; # = 6 # Second element of @ is the next ref to a pair-array $_; # = [1,9] $$_[0]; # 1 $$_[1]; # 9

But there are lots of other oddities. I believe the END sub will only get the first definition, so I believe it can only ever print out the '1 111 C T' results, which isn't overly helpful. And I never see it called. And using @g[1] is pointless, and should be $g[1], because it's a single element of an array, so you don't need it to be an array slice.

As corion said, if possible, ask the original author. Otherwise, I hope these hints have helped.


In reply to Re: Code clarification - use of map and $$_ by pryrt
in thread 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.