Perl Christmas Quiz

Uri Guttman uri at stemsystems.com
Fri Dec 12 17:29:24 GMT 2008


>>>>> "UG" == Uri Guttman <uri at stemsystems.com> writes:

>>>>> "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.

  UG> this appears to work fine and is a true one line sub.

  UG> use strict ;
  UG> use warnings ;

  UG> sub intersect {

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

and i squeezed out the white space and dropped the superfluous exists to
get this version:

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

it may not win in golf but it is a single expression/statement with no
declared variables so that should earn some points. i wouldn't call
multistatement code a true one liner. :)

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