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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.