I see no need to use each in this case:
sub find_in { my ($data_ref, $string) = @_; my $result = 'no result'; my ($key) = grep /^$string$/i, keys %$data_ref; $result = $$data_ref{$key}; return $result; }
Update: condensed and with a modified data set:
sub find_in { my ($data_ref, $string) = @_; return $$data_ref{ (grep /^$string$/i, keys %$data_ref)[0] || '' } + || 'no result'; } sub look_for_data { my %hash = ( A => 'axxx' , B => 'bxxx' , C => 'cxxx' , D => 'dxxx' +); print "d: " . find_in(\%hash , 'd') . "\n"; print "A: " . find_in(\%hash , 'A') . "\n"; print "B: " . find_in(\%hash , 'B') . "\n"; print "Z: " . find_in(\%hash , 'Z') . "\n"; } look_for_data(); __END__ d: dxxx A: axxx B: bxxx Z: no result
Update 2: Added ^ and $ anchors as per jhourcle advice.
Update 3: Same again.
--
David Serrano
In reply to Re: when do i know that the iterator for a hash was reseted
by Hue-Bond
in thread when do i know that the iterator for a hash was reseted
by rminner
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |