in reply to Re: Re: Uninitialized value ?
in thread Uninitialized value ?

I think your code should work correctly - in both versions. The only difference is that you get once a warning (which is exactly that, it's not an error) and iirc these early examples in merlyn's book (I suspect you are reading the llama) don't run under -w and strict because he hasn't introduced all the concepts yet.

In if ($words{$name} eq "") the key $name is looked up in the hash %words. This returns undef when the key $name does not exist in %words. The undef is then compared to "" with a string compare, so the undef is transformed into the empty string. This results in the comparision being true. But this might be a source of error, so perl warns you if you have warnings enabled (-w).

Why might that be a source of error? Well, imagine the empty string would be a valid entry in your hash, then you get the same result no matter if the key does not exist or the value is empty. Some code:

#/usr/bin/perl -w use strict; my %hash; $hash{key1} = ""; # this produces no warning if ($hash{key1} eq "") { # true print "key1 empty!\n"; } # this produces a warning if ($hash{key2} eq "") { # true print "key2 empty!\n"; }
to circumvent this look at defined (like suggested by busunsl) and for hashes also interesting exists.

-- Hofmator