my %a=('la'=>1, 'ka'=>5, 'pika'=>4); my %b=('pika'=>5, 'kasa'=>4, 'ka'=>44); my %common_keys=(); foreach my $key (keys %a) { if(exists $b{$key}) { $common_keys{$key}='yes'; } } my $the_common_keys_str=join(",", keys %common_keys); print "The common keys are: " . $the_common_keys_str;