I mentioned the FAQ because the only meaningful interpretation of "intersection" of two hashes is the intersection of their keys, which leads back to determining the intersection of two lists. I would code the loop a bit differently:
my @intersection = grep { exists $hash2{$_} } keys %hash1; my %intersection; @intersection{ @intersection } = @hash1{ @intersection };
Update: I made an error and had my %intersection where I should have assigned a hash slice instead. Fixed, thanks to ysth!
Update 2: Fixed a syntax error - you can't declare a hash slice via my. Spotted again by ysth!
In reply to Re^3: Intersect two hashes
by Corion
in thread Intersect two hashes
by HarshaHegde
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |