Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: code explanation

by DrHyde (Prior)
on Aug 04, 2011 at 10:39 UTC ( #918491=note: print w/ replies, xml ) Need Help??


in reply to code explanation

Read it from right to left.

@AoA (which from its name we can assume is an array of arrayrefs) is the input to grep. grep filters it, and spits out a list that is the same size or smaller than @AoA and that list gets put into @r.

The bit inside braces is how grep filters. Each value from @AoA in turn is assigned to $_ and then the contents of the braces are executed. If the result is true, the array element is spat back out, otherwise it isn't - it gets filtered out.

join $;, sort @$_ first dereferences $_, which is assumed to be an array reference, giving a list. That list is sorted. It is then glued together to make a string, using $; as the separator. $; is an obsolete hangover from perl4, whose default value is an unprintable ASCII character, see perlvar for details.

This string is then used as a key in the %uniq hash to look up a value or, if the key doesn't exist, create it. ++ is a pre-increment, meaning that the value we just got from the hash is incremented before being used. Finally the == compares that incremented value to 1.

The overall result is that @r contains only the unique elements of @AoA, where two elements are defined to be the same if they have the same number of elements with the same values but in any order.

The code is buggy, as it may fail if elements contain $;. It would be better to use something like Text::CSV_XS instead of the naïve join.


Comment on Re: code explanation
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://918491]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-09-23 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (227 votes), past polls