in reply to Re: grep keys in hash and retrieve values
in thread grep keys in hash and retrieve values

thank you. this is great, I thought of using a subroutine but was getting it wrong. But your script isn't working. To me it looks as if you are only passing the $prophage to the subroutine, but you must also pass the hash? I tried editing it as follows. But I am recieving an error about passing a string to a hash reference.

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

Replies are listed 'Best First'.
Re^3: grep keys in hash and retrieve values
by GrandFather (Saint) on Mar 16, 2012 at 12:35 UTC

    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