in reply to Comparing unordered hierarchical arrays
There are some efficincies to be had in the general approach. It is unclear if you want [ [1], [2] ] to be equal to [ [1,2] ]. Regardless the earlier you can fail the better for speed:
my $ha = [[1,2], [5,5,5], [1,2,], [3,4]]; my $hb = [[4,3], [1,2], [2,1,1], [5,5,5]]; # flatten my @ha = map{ @$_ } @$ha; my @hb = map{ @$_ } @$hb; # fail immediately unless equal num elements. if ( @ha != @hb ) { print "Not equal num items!\n"; } else { # now no choice but to sort or do detail compare if ( (join ' ', sort @ha) eq (join ' ', sort @hb) ) { print "Equ +al\n" } else { print "Not equal\n" } }
It is possible to use a hash to avoid the sort, even with duplicate values simply by counting occurences as implemented by BrowserUk
cheers
tachyon
|
|---|