Ok - here is a functional decomposition of "my @uniqueIPs = grep !$counts{$_}++, @ipAddresses;":
my @uniqueIPs; #Just declare it as an empty array; # The resulting value in %count is equivalent to the execution of t +his statement: $counts{$_}++ for @ipAddresses; # This is the important piece. # The hash %counts uses each ipAddress as a key. # The first time an IP address is encountered, the key-value pair is c +reated, with a value of zero. # the(++) increments that to 1. # The next time that IP is encountered, the value is incremented. So, + at the end, the value for each key # contains the count of occurrances for that IP address (key). ... grep !$counts{$_}++, @ipAddresses # The "grep:searches through each value ($counts{$_} is the VALUE), +and the negation (!) # looks for non-zero values. Because the operator used is post-increm +ent($blah++), before negation, # the value returnedwill be zero for First-seen IP addresses only. +For second and subsequent # sightings of the IP, the pre-negation value will be non-zero, an +d post-negation will be zero. # "grep" filters out, leaving only non-zero values, effectively retu +rning all the KEYS of %counts, which is # all the unique IP's.

     "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom


In reply to Re^3: Counting unique instances from Array Sort: explanation needed by NetWallah
in thread Counting unique instances from Array Sort by ewhitt

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.