in reply to map and grep one-liner problem
The complete result of a query for some key would have to be a list of matches. Using map() to generate such a result is reasonable, but let's look at your map again:
For one, why do you check for the key with grep() and then again in the map block? One of the tests can go.
Secondly, you are using s/// to change the given array element into just the value. Apart from being ugly, this changes your array @ta, so it wouldn't work again the next time. Print @ta after your code has run to see what I mean. Simply extract the value using regex capture.
Third (as has been noted), you want a semicolon instead of comma to separate the statements in the map block. The map block is evaluated in list context, so the comma is a list operator and map will collect both values. That isn't what you want, and it's the reason you need to select the second element from the result ((...)[1]).
Here is one way to do what you want:
If you only want the first hit, writemy @ta = ("a x", "b y", "c z", 'b yyy'); my $key = 'b'; my @values = map / (.*)/, grep /^$key /, @ta; print "@values\n";
Annomy ( $value) = map ...;
|
|---|