#!/usr/bin/perl -w use strict; my %first = ( aa => 1, bb => 2, cc => 3, ee => 6); my %second = ( bb=> 2, cc => 3, dd => 4 ); sub compare_one_hash_keys { my ($h1, $h2) = @_; return grep ( {not exists $h2->{$_} } keys %$h1) } sub compare_both_hash_keys { my ($h1, $h2) = @_; return grep ( {not exists $h2->{$_} } keys %$h1), grep ( {not exists $h1->{$_} } keys %$h2) ; } print compare_one_hash_keys( \%first, \%second), "\n"; print compare_one_hash_keys( \%second, \%first), "\n"; print compare_both_hash_keys( \%first, \%second), "\n"; __END__ prints: aaee dd aaeedd