Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Testing for key/value match

by cmilfo (Hermit)
on May 10, 2002 at 15:08 UTC ( [id://165662]=note: print w/replies, xml ) Need Help??


in reply to Testing for key/value match

Here is one way.
my $new_key = 'NEW'; my $new_value = 'Value'; if (exists $file_hash{$new_key} && $file_hash{$new_key} eq $new_value) { # add to another hash, per the comment in your post } else { $file_hash{$new_key} = $new_value; }
We are testing whether it exists or not to catch the case when $new_value is undefined and $new_key does not exist in the hash. If the $new_value == undef, then no matter what the key is (if it doesn't exist) a true value will be returned because the key in the hash HAS a value of undef.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-24 00:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found