Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I wanted to ask you something regarding the exists function in hashes. I have a problem when I want to search for a key like the following:
>G7USM1/27-197 [subseq from] G7USM1
Although it does exist in my hash, if I do:
if(exists($hash_tmbb{$hit_id}))
it doesn't find it... What am I doing wrong? Is it because of the special characters?

Replies are listed 'Best First'.
Re: Problem in finding hash keys
by GrandFather (Saint) on Mar 22, 2014 at 21:21 UTC

    Most likely if you show us a complete stand alone test script that shows the failure mode you will either discover the problem yourself (win mode 1) or we can reproduce it and show you where the issue is (win mode 2). Either way, spending 5 - 10 minutes to write the sample test script is a win. See I know what I mean. Why don't you?.

    If the code changes take longer than the time saved, it's fast enough already.
      Thanks to all of you! GrandFather was correct, I did a silly mistake in building the hash, so a small (but crucial) part of the ID was not included, thus no key could be found afterwards... It's a good practice to write the example code to post to the forum, indeed!
      Thank you!
Re: Problem in finding hash keys
by davido (Cardinal) on Mar 22, 2014 at 19:51 UTC

    Where is ">G7USM1/27-197 [subseq from] G7USM1" coming from, both when you turn it into a hash key, and later when you check for the key's existence? Is it possible that it is as simple an issue as always chomping your input, so that you're storing hash keys without trailing newlines, and so that the strings you're testing have no trailing newlines? Was the document from which you've derived these keys generated on a different operating system so that there's a line-ending mismatch?


    Dave

Re: Problem in finding hash keys
by CountZero (Bishop) on Mar 22, 2014 at 19:43 UTC
    There are no "special" characters for hash keys. On my system it finds a hash item with key ">G7USM1/27-197 [subseq from] G7USM1" without fail.

    Try a Data::Dump of your hash to make sure that entry indeed is present.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics