Stringifying the array can lead to incorrect results in some cases. Consider the following:
Prints:use strict; use warnings; use Digest::MD5 qw( md5 ); my @a1 = ( 'aa', 'a', '' ); print '[', join( '|', @a1 ), "]\n"; my $d1 = md5( @a1 ); my @sorted_a1 = sort @a1; print '[', join( '|', @sorted_a1 ), "]\n"; my $sorted_d1 = md5( @sorted_a1 ); printf( "Digests are %s\n", ( $d1 eq $sorted_d1 ) ? 'equal' : 'unequal' );
[aa|a|] [|a|aa] Digests are equal
In this case, the elements in the array were completely reversed by the sort but the md5 digest was identical.
This is (partly) documented in the docs for Digest::MD5:
The result of md5("a", "b", "c") will be exactly the same as the result of md5("abc").
In reply to Re: Fast way to check if a sort was done (using stringification is *broken*)
by bobf
in thread Fast way to check if a sort was doen
by Outaspace
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |