in reply to Compare/Diff on nested data structures

As GrandFather said, you are looking at non-trivial task. There are so many ways in which two "nested data structures" can differ, you would need to break the task down into stages:
  1. The "root" data types of the two structures have to be the same (can't compare an array to a hash)
  2. For the top-level unit, determine if the number of elements is same or different; for hashes, also determine which keys they have in common and which keys are unique to each hash
  3. Compare the values of the common elements:
    1. If the Nth elements in both arrays (or the values for key "X" in both hashes) are both scalars, do the two scalars match or not?
    2. If only one is a scalar and the other is a reference, how do you want to view that?
    3. If both are references, do they refer to the same type of thing?
      1. If so, you would probably check to see if they both actually point to the same memory address, and if they don't, recurse to compare the contents of each reference.
      2. If they point to different kinds of things, how do you want to view that?
  4. Figure out how you want to view the elements that are unique to one or the other structure, given that these things themselves could be arbitrarily complex.

Or, maybe you just need to be more explicit about the kinds of things you really want to compare (e.g. only isomorphic structures, where the possible differences are limited to "leaf-node" scalar values, or some similar constraint), and what purpose(s) would be served by your enumeration of differences (because the form of results should follow/support the intended function).

  • Comment on Re: Compare/Diff on nested data structures