I meant if a key exists in a hash.
Watch out to the vocabulary that you are using. These two things can be done to do something similar to what you want, but they don't do exactly the same thing:
$hash{'foo'} = 0 unless exists $hash{'foo'};
# Not the same as:
$hash{'foo'} = 0 unless defined $hash{'foo'};
If the hash key exists and the hash has a value for that key, nothing will happen in both cases. If the hash key does not exist, then the hash entry will be created in both cases. So no difference for these two cases.
There will be a difference if the hash key exists and no value is defined for that key entry. In this case, the first code line above will not modify the hash (since the hash key entry exists), while the second line will assign 0 to the value (since the value is not defined). Think carefully of what you really need. From your description of your need, you probably want the second line, but the correction you made quoted just above throws a bit of doubt into the picture. I still think you probably want the second form.
|