That's not exactly what it does. It's functionally the programmatic reverse of the array:
push @{ $First{ $A[$_] } }, $_ for (0..$#A);
Let's take it from the inside out:
$A[$_] -- the element of @A at the current index
$First{ $A[$_] } -- the previous line is a key in %First
@{ } -- and the associated value is an anonymous array
So what the code does is, loop over the number of elements of @A. (If it has ten, the loop goes from 0 to 9).
For each of those indices, the value at that index becomes a key in %First, and the value is the element's index, stored in an anonymous array. We have something like this:
Elements of @A:
0 zero
1 one
2 two
Elements of %First:
zero [ 0 ]
one [ 1 ]
two [ 2 ]
While we can look something up in @A by its index, we can look up the index in %First if we know the something.
Oh, and in Perl hashes have something called auto-vivification. If you access a hash key that doesn't exist (and try to put data there), the key will be created. (Useful, once you get used to it.)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.