[Purdue-pm] Can do better with map somehow?
Dave Jacoby
jacoby at purdue.edu
Mon Nov 29 06:34:45 PST 2010
There is something to that. I don't speak "map" as fluently as I should yet.
On 11/28/2010 03:36 PM, Mark Senn wrote:
> Dave Jacoby<jacoby at purdue.edu> wrote:
> My new solution:
>
> my $first = {
> 'a' => { 'foo' => 1, 'bar' => 'Cheers', },
> 'b' => { 'foo' => 3, 'bar' => 'Crow', },
> 'c' => { 'foo' => 1, 'bar' => 'Neon Cactus', } } ;
> my $second ;
> %$second =
> map { $_, $first->{ $_ } }
> grep { $first->{ $_ }->{ foo } == 1 } #<-- WOW!
> keys %$first ;
> say Dumper $second ;
>
> I think (but don't know for sure) Perl 5 will do the grep and
> save all those results and then do the map. For this small
> example it probably won't matter much but if %$first was very
> large the above may run out of memory.
>
> I prefer this:
>
> my $third;
> map { ($first->{$_}->{foo} == 1) and $third->{$_} = $first->{$_} }
> keys %$first;
> say Dumper $third;
>
> -mark
--
Dave Jacoby Address: WSLR S049
Code Maker Mail: jacoby at purdue.edu
Purdue University Phone: 765.49.67368
More information about the Purdue-pm
mailing list