in reply to The Best Hash

Dear Monks,

Some more answers to assist my question.

Dakkar:
Yes, @v1, @v2 etc .. would be permutation of others. The 'difference' between 2 same key-valued hashes is the total number of keys in a hash - total number of common keys-value pairs between two hashes.

Mojotoad:
Funny u said that, I cannot exactly figure out what u mean by those words. Still your assumption is close, as I my key/values are just text strings.

Waswas-fng:
Yes, the matches need to be exact and the keys and values are same in all the hashes. so there is no need for SOUNDEX like modules. I also explained the idea of the distance here. Which Modules would be helpful from CPAN?

More Notes:
I can take one hash 'H' at a time, compared to all others and and count the sum of the differences S(H) and finally can find 'H' with the least value of S(H). Anything better I can do?

Thanks,
Artist

Replies are listed 'Best First'.
Re: Re: The Best Hash
by graff (Chancellor) on Dec 14, 2002 at 02:07 UTC
    Yes, @v1, @v2 etc .. would be permutation of others. The 'difference' between 2 same key-valued hashes is the total number of keys in a hash - total number of common keys-value pairs between two hashes.

    It sounds like you might actually be trying to cluster the hashes based on relative similarity to each other -- or maybe you just want to identify the two hashes that are most similar to each other.

    Anyway, it might be interesting to treat the key-value pairs as strings, print the contents of each hash as a sorted list of these strings, then do pair-wise diffs on the text files. The pair with the fewest diffs is the closest match. The following has not been tested, but perhaps it will be useful.

    sub printHash { my ( $hash_name, $hash_ref ) = @_; open( LIST, ">", $hash_name ) or die $!; print LIST sort map { "$_ $$hash_ref{$_}\n" } keys %$hash_ref; close LIST; } ... # assume you have an "ur-hash", holding hash_refs keyed by hash_name: my @difflist1 = my @difflist2 = sort keys %all_hashes; foreach my $name ( keys %all_hashes ) { printHash( $name, $all_hashes{$name} ); } my %distances; foreach my $h1 ( @difflist1 ) { shift @difflist2; # dispose of the identical item in list2 foreach my $h2 ( @difflist2 ) { $distances{"$h1:$h2"} = `diff $h1 $h2 | grep -c '^<>'`; } } # then do something with the values in %distances

    (update: fixed to read $$hash_ref{$_} in the subroutine)