You probably wouldn't consider this a gracious way, but...
use strict; use warnings; use Data::Dumper; my %new_values; $new_values{MachineType}{Blue_machine}{Unit}{1}{Channel}{0}{Value}{15} + = 1; $new_values{MachineType}{Blue_machine}{Unit}{1}{Channel}{1}{Value}{22} + = 2; $new_values{MachineType}{Blue_machine}{Unit}{2}{Channel}{0}{Value}{19} + = 3; $new_values{MachineType}{Red_machine}{Unit}{1}{Channel}{0}{Value}{22} += 4; my %old_values; $old_values{MachineType}{Blue_machine}{Unit}{1}{Channel}{0}{Value}{14} + = 5; $old_values{MachineType}{Blue_machine}{Unit}{1}{Channel}{1}{Value}{22} + = 6; $old_values{MachineType}{Blue_machine}{Unit}{2}{Channel}{0}{Value}{21} + = 7; # Now find the difference if (my $end_values = hash_keydiff(\%new_values, \%old_values)) { print Dumper($end_values); } # level by level hash key compare # NOTE: at no time is the final hash value compared, only keys!! # returns 0 (no differences) # or a hash ref of the differences sub hash_keydiff { my ($hash1_ref, $hash2_ref) = @_; if (ref($hash1_ref) eq 'HASH') { return $hash1_ref unless ref($hash2_ref) eq 'HASH'; # iterate over hash1_ref keys my %return; foreach (keys %$hash1_ref) { if (defined $hash2_ref->{$_}) { # matching keys, dig to the next level if (my $result = hash_keydiff($hash1_ref->{$_}, $hash2_ref->{$_}) ) { $return{$_} = $result; } } else { $return{$_} = $hash1_ref->{$_} } } # iterate over hash2_ref keys # NOTE: we've already covered the matching keys above foreach (keys %$hash2_ref) { $return{$_} = $hash2_ref->{$_} unless defined $hash1_ref->{$_}; } return (keys %return) ? \%return : 0; } return (ref($hash2_ref) eq 'HASH') ? $hash2_ref : 0; }
BTW your example result is missing some differences:

$end_values{MachineType}{Blue_machine}{Unit}{1}{Channel}{0}{Value}{15}
$end_values{MachineType}{Blue_machine}{Unit}{2}{Channel}{0}{Value}{21}

In reply to Re: Get the difference between 2 Hashes by ruzam
in thread Get the difference between 2 Hashes by emat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.