in reply to Get the difference between 2 Hashes

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}