हाँ, यह कहा जाता है:
use List::Util qw<first>;
# Your regex does not compile perhaps you mean /teen$/
my $value = $hash{ (first { m/teen/ } keys %hash) || '' };
(स्मार्ट मैच से पहले, वह यह है कि स्मार्ट मैच के लिए भीड़ का जवाब देखें।।)
आप कुंजी को भी सॉर्ट कर सकता है:
my $value = $hash{ (first { m/teen/ } sort keys %hash) || '' };
मैं एक "ऑपरेशन" में इस फ्रीज होगा:
use Scalar::Util qw<reftype>;
sub values_for_keys_like (\[%$]$) {
my $ref = reftype($_[0]) eq 'HASH' ? $_[0] : $$_[0];
return unless my @keys = keys %$ref;
my $regex = shift;
# allow strings
$regex = qr/$regex/ unless my $typ = ref($regex);
# allow regex or just plain ol' filter functions.
my $test = $typ eq 'CODE' ? $regex : sub { return unless m/$regex/; 1 };
if (wantarray) {
return unless my @k = grep { defined $test->($_) } @keys;
return @$ref{ @k };
}
else {
return unless my $key = first { defined $test->($_) } @keys;
return $ref->{ $key };
}
}
और तुम इतना है कि यह इस्तेमाल कर सकते हैं:
my $key = values_for_keys_like(%hash => qr/teen/);
या
my $key = values_for_keys_like($base->{level_two}{level_three} => qr/teen/);
यदि आपके पास 'किशोर $' '*' अनिवार्य है। – Axeman