in reply to Comparing unordered hierarchical arrays

Update: I misread your post. Sorry.

This might be a little quicker as it avoids the sorting

#! perl -slw use strict; sub cmpAoAs{ my( $a, $b ) = @_; my %h; $h{ $_ }++ for map{ @$_ } @$a; $h{ $_ }-- for map{ @$_ } @$b; return 0 == grep{ $_ } values %h; } my $ha = [ [1,2], [5,5,5], [1,2], [3,4] ]; my $hb = [ [4,3], [1,2], [2,1], [5,5,5] ]; print 'Same' if cmpAoAs $ha, $hb; __END__ P:\test>342841 Same

However, on the surface of what you've told us, it loooks a little like your using the wrong structure if only the presence and count of a value (rather than it's position) determine equivalence. But I realise that I am not party to the full sp on your application:)


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

Replies are listed 'Best First'.
Re: Re: Comparing unordered hierarchical arrays
by Zaxo (Archbishop) on Apr 06, 2004 at 06:47 UTC

    I don't think

    my $ha = [ [1,2], [5,5,5], [1,2], [3,4] ]; my $hb = [ [1,3], [5,5,5], [1,2], [2,4] ];
    are supposed to compare equal, as your function will return.

    After Compline,
    Zaxo

Re: Re: Comparing unordered hierarchical arrays
by ant9000 (Monk) on Apr 06, 2004 at 06:51 UTC
    Your solution seems flawed to me: consider
    my $ha = [ [1,2], [5,5,5], [1,2], [3,4] ]; my $hb = [ [4,3], [1,2], [2,1,5], [5,5] ];
    These are clearly different, but your code still outputs "same" since it is discarding the entire original structure (and not only the order of elements).