Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: find differences between multiple hashes

by kvale (Monsignor)
on Apr 10, 2004 at 02:43 UTC ( #344071=note: print w/replies, xml ) Need Help??

in reply to find differences between multiple hashes

Comparing two general hierarchical data structures is in general a hard problem. First, you have to establish a criterion for equivalency. Do hash values have to be exactly the same, or is it the values' contents? Must arrays have exactly the same elements in the same order, or is it that they form equivalent sets good enough? Second, you have to come up with a search strategy.

For instance, for a hash of hashes and assuming $retrieved is a superset of $given, the following can be used:

my $result = {}; foreach my $main_key (keys %$retrieved) { unless (exists $given->{$main_key} ) { $result->{$main_key} = $retrieved->{$main_key}; next; } # The key exists, compare subhashes foreach my $sub_key (keys %$main_key) { $result->{$main_key}{$sub_key} = $retrieved->{$main_key}{$sub_ke +y} unless exists $given->{$main_key}{$sub_key} && $given->{$main_key}{$sub_key} eq $retrieved->{$main_key +}{$sub_key}; } }
The idea is that given your data structure and equivalence criteria, you can drill down and simply do comparisons, rather than deletions. This should be quicker. For your particular application, I cannot discern your equivalence criterion, so I'll stop here.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://344071]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2023-02-06 03:46 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (33 votes). Check out past polls.