#!/usr/bin/perl -w use strict; my (%hash1, %hash2); # Defined elsewhere in the program for my $key_h1 (keys %hash1) { my $val_h1 = $hash1{$key_h1}; for my $key_h2 (keys %hash2) { my $val_h2 = $hash2{$key_h2} # You can now check $key_h1/2 & $val_h1/2 # And safely modify %hash1 or %hash2 } }