Following on moritz's comment, I'll point out what is likely happening. Since your iterator is never reset and you are looping over a hash, every few times your call your subroutine (actual frequency depends on what letters you pass it), you have exhausted the iterator on your hash. Since your return statement only gets executed with a successful match, you exit your subroutine without explicitly specifying a value. Perl therefore returns the result of the last evaluation, which I think is the logical state of the each statement - that is why you get zeros.

I'd also point out that $key and $value are not localized to the loop (probably ought to read my ($key, $value)). Assuming you are using and passing strict, this means you have actually created a closure and are possibly clobbering similarly named variables elsewhere in the code.


In reply to Re: File Reading and Hash Accessing that isn't doing what you'd expect. by kennethk
in thread File Reading and Hash Accessing that isn't doing what you'd expect. by Chris_Stevens

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.