Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re: Uninitialized value ?

by All0uette (Acolyte)
on Aug 20, 2001 at 17:32 UTC ( [id://106215]=note: print w/replies, xml ) Need Help??


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

busunsl, Thanks for that - it seems to work - although I'm still not sure why the original didn't. Especially as I got this form the book ! Any chance you can explain a lttle more. Cheers.

Replies are listed 'Best First'.
Re: Re: Re: Uninitialized value ?
by Hofmator (Curate) on Aug 20, 2001 at 17:59 UTC

    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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (1)
As of 2024-04-25 04:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found