TJRandall has asked for the wisdom of the Perl Monks concerning the following question:

I have two deep hashes (represented below) - I want to compare the two against each other to create a database difference report. So when I'm finished (against the code below), I would know that I have two column diffs between them ('SOME_DATE' has different type and 'CYCLE_DATE' has different nullable values), as well as I have an extra column in the second hash. I've been using the examples here to walk through the hashes, but I can't see how to effectively compare them / place all the differences into a third array/hash: "differences". Thank you for any/all help that you can provide!
#!/usr/bin/perl -w use strict; use Data::Dumper; my %hash = ( TABLE_NAME => { COLUMN_NAME => { 'CYCLE_DATE' => { TYPE => 'VARCHAR2(20)', NULLABLE => 'N' +}, 'SOME_DATE' => { TYPE => 'DATE', NULLABLE => 'N' +}, 'AMITRUE' => { TYPE => 'BOOLEAN', NULLABLE => 'N' +} } } ); my %hash2 = ( TABLE_NAME => { COLUMN_NAME => { 'CYCLE_DATE' => { TYPE => 'VARCHAR2(20)', NULLABLE => 'Y' +}, 'SOME_DATE' => { TYPE => 'SOMEGUY', NULLABLE => 'N' +}, 'AMITRUE' => { TYPE => 'BOOLEAN', NULLABLE => 'N' +}, 'THE_DUDE' => { TYPE => 'VARCHAR2(10)', NULLABLE => 'Y' +} } } ); # works for walking single hash - have just started # trying to figure out how I'd do both. walk_hash(\%hash, \%hash2); sub walk_hash { my ($h1, $h2) = shift; foreach my $key (keys %$h) { if( ref $h->{$key}) { walk_hash( $h->{$key} ); } else { print $h->{$key}; } } }

Replies are listed 'Best First'.
Re: Comparing two deep hash of hashes
by Tux (Canon) on Sep 16, 2011 at 13:23 UTC

    If you do not need detailed output, but a simple "flag" of same/diff, Test::More offers

    is_deeply (\%hash1, \%hash2, "Comparing my hashes");

    If you need a more in-depth analysis, Test::Differences will show you the diffs

    eq_or_diff (\%hash1, \%hash2, "Comparing my hashes");

    alternative formatting is available in the functions table_diff, unified_diff, context_diff, oldstyle_diff, eq_or_diff_text and eq_or_diff_data.


    Enjoy, Have FUN! H.Merijn
Re: Comparing two deep hash of hashes
by muba (Priest) on Sep 16, 2011 at 13:30 UTC

    There's a typo in the code you provided.

    sub walk_hash { my ($h1, $h2) = shift; foreach my $key (keys %$h) { # I presume you mean %$h1 here? if( ref $h->{$key}) { # And $h1->... here? walk_hash( $h->{$key} ); # and here? } else { print $h->{$key}; # and here as well? } } }

    Other than that, I suggest you check the modules Tux pointed at, above.

      Hi, This code is not working Thanks, Lakshmareddy.
Re: Comparing two deep hash of hashes
by raybies (Chaplain) on Sep 16, 2011 at 13:32 UTC

    You appear to be sending $hash and $hash2 into local vars, $h1 and $h2, but then using a variable called $h. Not sure where $h ever gets assigned to anything.

    I think if you "use warnings;" this would be more obvious.

      Sorry about the typo-s - I was clipping down to make it short and sweet post. I will follow the above advice in Test:More - Thank you!!