Is there a correct way of testing if a particular hash key has already been assigned any value, even if this value is zero? I have been working with a statement like this:
%hash=();
$hash{a}=1;
$hash{b}=0;
if($hash{$key}) { do something }
but this gives the same result for keys that have neven been touched and those that have been assigned the value 0 (e.g. both $hash{b} and $hash{c} evaluate as ‘false’). Is there a way of telling the difference between those two ?
Use the defined operator to check if something has value which is not
undefUse the exists operator to check if a
$keyof a%hashhas been written toThe difference being that
definedchecks to see if a value is of anything other thanundefandexistsis used to verify if$keyis a key of the hash despite its value.