Re: comparing nested data structures
by CountZero (Bishop) on Nov 02, 2011 at 10:19 UTC
|
I think Test::Deep might solve your problems, or at the very least its code could give you some ideas to implement it yourself.Deep::Hash::Utils also gives some "easier" access to deeply nested hashes and arrays, without the need to write yourself the recursive routines.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] |
|
|
Very good, thanks a lot! :)
| [reply] |
Re: comparing nested data structures
by JavaFan (Canon) on Nov 02, 2011 at 10:17 UTC
|
| [reply] [d/l] |
|
|
| [reply] |
Re: comparing nested data structures
by GrandFather (Saint) on Nov 02, 2011 at 10:09 UTC
|
So how is dumping the structures as text (which has to traverse the structures in any case) then parsing the text to recreate some internal representation to allow comparison of the structures going to be faster than a direct comparison?
True laziness is hard work
| [reply] |
|
|
| [reply] |
|
|
If the dumping and diffing can be done using XS, then so can the recursive comparison.
| [reply] |
|
|
Re: comparing nested data structures
by keszler (Priest) on Nov 02, 2011 at 10:15 UTC
|
| [reply] |
|
|
Or if you want to compare arrays as well (and hashes of arrays, and arrays of hashes) then Data::Compare. It won't tell you what the differences are though.
| [reply] |
|
|
| [reply] |
Re: comparing nested data structures
by BrowserUk (Patriarch) on Nov 02, 2011 at 11:33 UTC
|
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.
| [reply] |