Perl Christmas Quiz

Uri Guttman uri at stemsystems.com
Fri Dec 12 18:54:58 GMT 2008


>>>>> "J" == Jasper  <jaspermccrea at gmail.com> writes:

  UG> sub intersect {
  >> 
  >> return keys%{{map{$_,1}grep(${{map{$_=>1}@{$_[0]}}}{$_},@{$_[1]})}} ;

  J> Uri, I don't believe this does the right thing with regard to multiple
  J> occurrences of an entry in both arrays. I may be wrong.

sub intersect {

	return keys%{{map{$_,1}grep(${{map{$_=>1}@{$_[0]}}}{$_},@{$_[1]})}} ;
}

my @x = ( 1 .. 5 ) ;
my @y = ( 4 .. 8, 4 .. 8 ) ;

print intersect( \@x, \@y ), "\n" ;

that prints 45 as expected. the outer hash ref and keys does the
uniquifying.


  J> I think this works

  J> sub intersect {grep(!++$_[2]->{$_},@{$_[0]}),grep 1x$_[2]->{$_}--,@{$_[1]}}

  J> Bit of a cheat using $_[2], though

yeah, using @_ to hold a temp hash. as i said, i think mine is correct
as is.

and removing the return and converting another => to , makes it shorter:

	keys%{{map{$_,1}grep(${{map{$_,1}@{$_[0]}}}{$_},@{$_[1]})}} ;

uri

-- 
Uri Guttman  ------  uri at stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


More information about the london.pm mailing list