in reply to Re^2: common keys in two hashes
in thread common keys in two hashes
#!/usr/bin/env perl use warnings; use strict; my %hash1 = (a=>1,b=>2,c=>3,d=>4); my %hash2 = ( b=>5,c=>6, e=>7); my @common = (); foreach (keys %hash1) { push(@common, $_) if exists $hash2{$_}; } # @common now contains common keys print "common1: @common\n"; @common = (); # empty the array # reverse the search: foreach (keys %hash2) { push(@common, $_) if exists $hash1{$_}; } print "common2: @common\n";
prints:
common1: c b common2: c b
|
|---|