in reply to Compare complex perl data structures
If each hash referent in an array contains information about a unique platform and if each platform is uniquely identified by, say, a 'platformid' => ... (or some other such) number, it may be enough to sort just by these unique platform identifier numbers:
Of course, if you have an array likemy @array1 = ( {'platformid' => '22', ... }, {'platformid' => '100', ... }, ..., ); my @ordered_array1 = sort { $a->{platformid} <=> $b->{platformid} }@ar +ray1; # ascending numeric sort ...; my $rc = Compare(\@ordered_array1, \@ordered_array2); ...;
this is not going to work.my @array1 = ( {'platformid' => '22', 'some' => 'stuff', ... }, {'platformid' => '100', ... }, ..., {'platformid' => '22', 'other' => 'things', ... }, ..., );
Note also that things like 'platformid' => '22' that seem to be numbers should be numerically compared with the <=> operator. Lexical (string-wise) comparison of numbers with the cmp operator will give strange results.
Another point is that if you really need to use an enormously complex comparison like
and use it in more than one place, then this comparison can and should be encapsulated in a sanity-saving function:my @array3 = sort { $a->{platformid} cmp $b->{platformid} or $a->{da} cmp $b->{da} or $a->{ma} cmp $b->{ma} or $a->{os} cmp $b->{os} or $a->{cc} cmp $b->{cc} or $a->{objecttype} cmp $b->{objecttype} or $a->{host} cmp $b->{host} or $a->{size} cmp $b->{size} } @array1;
Note that the value of the 'size' key is an array reference, and comparing references as numbers (or strings) is problematic: exactly what is the point of this comparison?sub enormously_complex_compare { $a->{platformid} <=> $b->{platformid} # numeric? or $a->{da} cmp $b->{da} or $a->{ma} cmp $b->{ma} or $a->{os} cmp $b->{os} or $a->{cc} cmp $b->{cc} or $a->{objecttype} <=> $b->{objecttype} # numeric? or $a->{host} <=> $b->{host} # numeric? or $a->{size} <=> $b->{size} # ??? comparing array references ??? } my @array3 = sort enormously_complex_compare @array1; my @array4 = sort enormously_complex_compare @array2; ...
Give a man a fish: <%-{-{-{-<
|
|---|