Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: comparing nested data structures

by BrowserUk (Pope)
on Nov 02, 2011 at 11:33 UTC ( #935358=note: print w/replies, xml ) Need Help??

in reply to comparing nested data structures

would like to join indentical sub-trees

It kind of depends what you mean by "identical subtrees".

If by identical, you mean that any substructure references below the top level are references to the same hash or array , then simply stringifying both hashes (or both arrays) and comparing the string for equality will tell you if they are identical or not. And it will be considerably faster than full tree traversal methods.

AFAIK (and my ability to quickly verify), any hash that contains the same keys and values will stringify(*) to the same string, regardless of either the order in which the hash was constructed or whether it has at anytime contains other keys subsequently removed. So there should be no need to sort the keys.

If you would consider two different arrays or hashes that contains the same data as "identical", then it would be necessary to recursively stringify contained references bottom up. It might well still be quicker than an element by element recursive comparison if all you need is a boolean yes/no rather than a blow-by-blow differences found.

(*using suitable delimiters.)

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://935358]
and the voices are still...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-12-17 18:59 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (466 votes). Check out past polls.