Perl Christmas Quiz

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

>>>>> "UG" == Uri Guttman <uri at> writes:

>>>>> "CJ" == Chris Jack <chris_jack at> 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 Guttman  ------  uri at  -------- --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- ---------
---------  Gourmet Hot Cocoa Mix  ---- ---------

More information about the mailing list