in reply to Re: Counting unique instances from Array Sort
in thread Counting unique instances from Array Sort
Okay, I've seen and used this a hundred times, but I have to come clean...I have no idea what is going on "under the hood" with this:
my %counts; my @uniqueIPs = grep !$counts{$_}++, @ipAddresses;
I'd like to have someone explain it to me in a sentence or two. But let me take a stab at it first:
1. The first element in the list @ipAddresses becomes the first key in the hash %counts.
2. And because the first element of @ipAddresses ($_) is not equal to the first element of @uniqueIPs (mainly because it's empty), the first element of @ipAddresses ($_) is "pushed" onto @uniqueIPs.
3. HERE'S WHERE I GET LOST: What is incrementing that value? The fact that it satisfies the condition of not matching, therefore being true? And let's say the next element in @ipAddresses is the same as the first, and because it IS equal to the first element of @uniqueIPs it is *not* pushed. QUESTION: Why would the value of that first key get incremented.
So, is the condition asking "if it is not equal then increment it?"
What am I not getting? Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Counting unique instances from Array Sort: explanation needed
by ikegami (Patriarch) on Jan 07, 2008 at 17:35 UTC | |
by bradcathey (Prior) on Jan 07, 2008 at 19:09 UTC | |
by ikegami (Patriarch) on Jan 07, 2008 at 20:19 UTC | |
|
Re^3: Counting unique instances from Array Sort: explanation needed
by Anonymous Monk on Jan 07, 2008 at 14:20 UTC | |
|
Re^3: Counting unique instances from Array Sort: explanation needed
by NetWallah (Canon) on Jan 07, 2008 at 17:00 UTC |