Perl Christmas Quiz
Uri Guttman
uri at stemsystems.com
Fri Dec 12 16:58:08 GMT 2008
>>>>> "CJ" == Chris Jack <chris_jack at msn.com> writes:
CJ> 3) Write a Perl function that takes two references to arrays and
CJ> returns the intersect of them. If an entry appears n times in
CJ> array 1 and m times in array 2, the output should list that entry
CJ> min(n,m) times. Bonus mark for one line solutions.
this appears to work fine and is a true one line sub.
use strict ;
use warnings ;
sub intersect {
return keys %{{ map {$_, 1} grep(exists ${{map { $_ => 1 } @{$_[0]}}}{$_}, @{$_[1]}) }} ;
}
my @x = ( 1 .. 5 ) ;
my @y = ( 4 .. 8, 4 .. 8 ) ;
print join( ' ', intersect( \@x, \@y )), "\n" ;
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