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

Re: [MacPerl-AnyPerl] an elegant uniquing



At 7:35 PM -0800 2/5/00, 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;
>
>[but] grep is returning "$_", not "${$_}{names}" as I intended.
>
>Is there a way to do this in one line?

Map returns the processed values.

(I think you meant to have another curly bracket before the ++ :)

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

[Still a one-liner, I just hit return for intelligible line-wrapping...]

But ??, since you use the plural-sounding constant 'names' as the key in
the de-referenced hash %{$_}, are you getting lists of names, once
de-referenced? Or lists of references? Guess I haven't quite pictured how
you've organized the data...


- Bruce

__Bruce_Van_Allen___bva@cruzio.com__831_429_1688_V__
__PO_Box_839__Santa_Cruz_CA__95061__831_426_2474_W__