[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl-AnyPerl] an elegant uniquing



On Sat,  5 Feb 2000 22:35:48 -0500, Nathaniel Irons wrote:

>This is a neat one-line trick I got from the Cookbook:
>
> @unique = grep { ! $seen{ $_ }++ } @not_unique;
>  
>which I assumed I could adapt to my needs:
>
> @unique_names = grep { ! $seen{ ${$_}{names}++ } @array_of_hashes;
> 
>which works, in that I get the right number of elements, but fails in
>that my new @u_names array is full of hash references, and not stringy
>hash values.  grep is returning "$_", not "${$_}{names}" as I intended.
>
>Is there a way to do this in one line?

Use grep()'s nefew: map().

   @unique_names = grep { !$seen{$_}++ }
   	map { $_->{names} }
   	@array_of_hashes;

-- 
	Bart.

==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org