in reply to Re^2: Intersect two hashes
in thread Intersect two hashes
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Intersect two hashes (initialize hash slice)
by ysth (Canon) on Jun 11, 2007 at 07:00 UTC | |
|
Re^4: Intersect two hashes
by GrandFather (Saint) on Jun 10, 2007 at 21:19 UTC |