in reply to Using Data::Compare recursively to better identity differences between hashes

There is also Data::Difference that compares hash & hash references, and shows where a difference exist.

BTW, Data::Compare does have the facility to compare array references (see "ignore_hash_keys" option).

Some time ago I wrote some code to dump the two data structures (via Data::Dumper) to two separate files (File::Temp) followed by file-difference (possibly Algorithm::Diff or Text::Diff; OTOH might have been a call to system diff(1)).

For visual difference, see Text::ParagraphDiff (never used myself). Also, GNU diff (version: diff (GNU diffutils) 2.8.1) has the facility to show word differences, optionally in color.

  • Comment on Re: Using Data::Compare recursively to better identity differences between hashes

Replies are listed 'Best First'.
Re^2: Using Data::Compare recursively to better identity differences between hashes
by Anonymous Monk on Oct 18, 2014 at 18:25 UTC

    I am unsure how to compare arrays of hashes ... -- Lady_Aleena

    The comment "Data::Compare does have the facility to compare array references" does not apply with respect to the above doubt. With combination of ...

    # I have not figured out this part yet. # elsif (ref($old) eq 'ARRAY' && ref($new) eq 'ARRAY') { # }

    ... it seems you are unsure about comparison of array reference of hash references. (Loose language sinks ships. ;-) Regardless, you would need to loop over array (reference) elements near the beginning of the sub ...

    sub deep_data_compare { my ( $old, $new ) = @_; if (ref $old eq 'ARRAY' && ref $new eq 'ARRAY') { # Handle the case of uneven number of elements in # each array reference as you see fit. Here assumption # is that both references have the same number of # elements. for my $ix ( 0 .. $#{ $old } ) { deep_data_compare( map $_->[ $ix ], $old, $new ) } } ... }
      i just saw a tweet that said foreach \%hash ( @AoH ) { ... } is coming to perl5.22 (available in bleed now).