in reply to from array to hash with grep
There must be a logical technical reason to how it should/could work. grep reads a list and checks for which ones the condition evaluates to true. That's all. That doesn't look very useful.
Now you're try this with map, it'll build a list of return values instead. Now what would the value returned from the regexp be? Why, you have capturing parens, map calls the regexp in list context, so it'll return an empty list for no match, and a list of captured values for matches. So
will put ('A', 'B', 'C') into the array, for your particular input.@result = map /\w{3}_(\w)/, @inp;
If you foreach through that list, you can use each as a hash key, in turn. So this will do what you want:
$inp{$_} = 1 foreach map /\w{3}_(\w)/, @inp;
It looks close to what you made up, but it isn't. Not really.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: from array to hash with grep
by jeanluca (Deacon) on Jun 15, 2006 at 08:18 UTC | |
by bart (Canon) on Jun 15, 2006 at 08:27 UTC | |
by davorg (Chancellor) on Jun 15, 2006 at 08:27 UTC |