Let me see if I understand your one-liner...

*  the "map" function creates a hash, using the array elements as key names,
automatically eliminating duplicates
*  the output from the "keys" function (an array) is compared to the
original array, both in scalar context, so the number of elements will be
different if there were duplicates

Pretty neat.  So the difference between this an the iterative approach is
that the one liner processes all the array elements every time and the
iterative approach drops out as soon as it finds a duplicate.

-----Original Message-----
From: Bob Showalter [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 31, 2001 09:03
To: '[EMAIL PROTECTED]'
Subject: RE: check array element (HELP)


> -----Original Message-----
> From: Mooney Christophe-CMOONEY1 [mailto:[EMAIL PROTECTED]] 
> Sent: Tuesday, July 31, 2001 9:44 AM
> To: [EMAIL PROTECTED]
> Subject: RE: check array element (HELP)
> 
> 
> I would probably do this, although i'm sure some smarty-pants 
> could come up with a one-liner  ;)
> 
> sub repeated_elements
> {
>       my %found_one;
>       for (@_)
>       {
>               return 1 if $found_one{$_}++;
>       }
>       return 0;
> }

One-liner:

   print "Duplicates!" if keys %{{ map { ($_ => 1) } @arr }} != @arr;

Note: Do NOT use this! Along with being quite unreadable, it's also
inefficient. Use the iterative approach above.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to