Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: find differences between multiple hashes

by kvale (Monsignor)
on Apr 10, 2004 at 02:43 UTC ( [id://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.

-Mark

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-03-28 22:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found