in reply to Re^2: Counting unique instances from Array Sort: explanation needed
in thread Counting unique instances from Array Sort
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
|
|---|