in reply to map and grep one-liner problem
use strict; use warnings; my @ta = (q{a x}, q{b y}, q{c z}); my $key = q{b}; print qq{key = $key, value is @{[getValue($key)]}\n}; sub getValue { my $key = shift; return map { $_->[1] } grep { $_->[0] eq $key } map { [ split ] } @ta; }
The output is
key = b, value is y
I hope this is of use.
Cheers,
JohnGG
Update: Thinking about it, you could do most of the work inside the print statement, getting rid of the subroutine
use strict; use warnings; my @ta = (q{a x}, q{b y}, q{c z}); my $key = q{b}; print qq{key = $key, value is @{ [ map { $_->[1] } grep { $_->[0] eq $key } map { [ split ] } @ta ] }\n};
This produces the same output as the first version.
|
|---|