
Laurent_R
Veteran
/ Moderator
Oct 3, 2013, 10:43 AM
Post #4 of 5
(2921 views)
|
Re: [alferic] How do I get elements of a hash whose keys match a given regex?
[In reply to]
|
Can't Post
|
|
Just as an additional information on the errors in your code, you don't quite understand the grep syntax. The grep function takes a list of values on its right and passes on its left the values for which the grep block returned true. Second point, your $regex is not initialized correctly, you should use the assignment operator, not the match operator for that. So you could have done this:
my $regex = qr/queen/; my @matching_keys = grep {/$regex/} keys %hash; Now, the @matching_keys array contains the values which matched the regex (just one, queen, in your case).
|