Err, I did say it was untested didn't I?

There are two errors related to the hash. The second one is that the return statement needs to be changed to:

return $prophage, $host, $lu->{$matches[0]};

to return the value instead of the key.

The first problem is as you noticed, the hash needs to be passed to the sub, but it needs to be passed by reference because of the way the code in the sub works:

my ($prophageB, $hostB, $taxidB) = getTaxId($columns[0], \%tax2loc +);

The pass by reference is an optimisation to save passing all the keys and values of the hash in a list which is what would happen otherwise. Note that the point of passing the hash at all is to avoid treating it as a global variable which is generally a "bad thing"™ (although this is such a small program it's not an issue except as a style thing).

True laziness is hard work

In reply to Re^3: grep keys in hash and retrieve values by GrandFather
in thread grep keys in hash and retrieve values by AWallBuilder

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.