http://www.perlmonks.org?node_id=471029


in reply to Finding an intersection of two sets, lean and mean

Uhh ... you do realize that you just implemented a very rudimentary hash table in Perl using ... wait for it ... hashes! Except, you don't handle collisions. What's wrong with:
sub intersection { my ( $list1, $list2 ) = @_; my %hash; @hash{@$list1} = undef; my @intersection = grep { exists $hash{$_} } @$list2; return @intersection }

Update: Fixed mistake in my code (map -> grep) Compare mine to yours and I think you'll notice a difference.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?