Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Best way check key availability

by banduwgs (Beadle)
on Sep 11, 2003 at 16:30 UTC ( [id://290738]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks,
Following piece of code is not work as obvios
use strict; my %hash = ( 'key01' => '1', 'key02' => '2', 'key03' => '3', ); my $key = 'key01'; if ($key in keys(%hash)) { ### print "$key is in hash"; }
But my question is what is the optimal way to implement the functionality of "in" at "### in the above code (without looping entire array.

Thanks in advance - SB.

Replies are listed 'Best First'.
Re: Best way check key availability
by liz (Monsignor) on Sep 11, 2003 at 16:35 UTC
    exists()

    my $key = 'key01'; if (exists $hash{$key}) { print "$key is in hash"; }
    If the values of in the hash can all be considered "true" (as in your example), then you could simplify this further to:
    my $key = 'key01'; if ($hash{$key}) { print "$key is in hash"; }

    Liz

      If the values of in the hash can all be considered "true" (as in your example), then you could simplify this further

      Not really.

      If the purpose of the quest is to find out if the key exists, then your second example will have an undesired side effect, because it will create 'key01' even if it didn't exist before. So the test is correct, but not its meaning, because key01 will be there, holding an undef value and ready to do havoc next time you use it.

        Nope. It won't. Observe:
        use strict; my %a; if ($a{'foo'}) {} print keys %a;

        According to your claim, this should print "foo". Well, it doesn't ;-)

        Liz

Re: Best way check key availability
by naChoZ (Curate) on Sep 11, 2003 at 16:36 UTC
      That Q&A reference explains to the original poster how to see if a particular value is in a hash, or if a particular hash key contains a specific value.

      The OP's question was, what is the proper way to check for the existance of a particular hash key, which is quite different from the answer implied in your response.

      If his objective is to verify that $hash{key} has been created (and thus exists), he should use the construct that Liz provided: if (exists $hash{key} ) { ....do your stuff.... }. Again, exists will tell you if a hash key has been brought into existance, which seems to be the answer the OP was asking for.

      On the other hand, if someone is interested in whether a particular key's value has been defined, one would use the defined function as in, if (defined $hash{key} ) { ....do your stuff....}

      Defined differs from exists in that defined returns true if the hash's key's value has been defined, and false if the hash's key's value is undef. But it doesn't care whether or not the hash's key has been created or not. In other words, using defined, you don't know if it is returning false because the key is nonexistant, or because the value referred to by the key is undef. It makes no distinction. Defined is also applicable to other forms of scalars and function returns even.

      Hope this helps.

      Dave

      "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://290738]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-03-29 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found