Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://918491]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2017-03-30 01:53 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.