|Perl: the Markov chain saw|
Re: code explanationby DrHyde (Prior)
|on Aug 04, 2011 at 10:39 UTC||Need Help??|
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.