Nov 2, 2005 #1 r2d22y Technical User Joined Nov 30, 2004 Messages 46 Location SE Hello Is there a difference between: if(defined $myvalue){ } and if($myvalue){ } What I want to control is if $myvalue has a value or not $myvalue is a Hashvalue that maybe isnt set so thats wy I have to control if it exists. Regards/D_S
Hello Is there a difference between: if(defined $myvalue){ } and if($myvalue){ } What I want to control is if $myvalue has a value or not $myvalue is a Hashvalue that maybe isnt set so thats wy I have to control if it exists. Regards/D_S
Nov 2, 2005 #2 TrojanWarBlade Programmer Joined Apr 13, 2005 Messages 1,783 Location GB Yes, there is a BIG difference. Your second example tests for "truth". 0, "" and undef are all considered false and everything else is true. In your first example, ONLY undef is false, everything else (including 0 and "") is true. Trojan. Upvote 0 Downvote
Yes, there is a BIG difference. Your second example tests for "truth". 0, "" and undef are all considered false and everything else is true. In your first example, ONLY undef is false, everything else (including 0 and "") is true. Trojan.
Nov 2, 2005 #3 tchatzi Technical User Joined Dec 15, 2004 Messages 744 Location GR if in your code you have Code: $foo{key} = 0; $myvalue = $foo{key}; if (defined $myvalue){print "defined\n";} it will print 'defined ' Code: $myvalue = 0; if (defined $myvalue){print "defined\n";} it will print 'defined ' Code: $myvalue = $foo{key}; if (defined $myvalue){print "defined\n";} it will print nothing on the other hand Code: $myvalue = $foo{key}; if ($myvalue){print "it has something in it\n";} it will print nothing Code: $myvalue = 0; if ($myvalue){print "it has something in it\n";} it will print nothing Code: $myvalue = "whatever"; # except 0 or '' if ($myvalue){print "it has something in it\n";} it will print "it has something in it" Did you get the difference? ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI Upvote 0 Downvote
if in your code you have Code: $foo{key} = 0; $myvalue = $foo{key}; if (defined $myvalue){print "defined\n";} it will print 'defined ' Code: $myvalue = 0; if (defined $myvalue){print "defined\n";} it will print 'defined ' Code: $myvalue = $foo{key}; if (defined $myvalue){print "defined\n";} it will print nothing on the other hand Code: $myvalue = $foo{key}; if ($myvalue){print "it has something in it\n";} it will print nothing Code: $myvalue = 0; if ($myvalue){print "it has something in it\n";} it will print nothing Code: $myvalue = "whatever"; # except 0 or '' if ($myvalue){print "it has something in it\n";} it will print "it has something in it" Did you get the difference? ``The wise man doesn't give the right answers, he poses the right questions.'' TIMTOWTDI
Nov 2, 2005 Thread starter #4 r2d22y Technical User Joined Nov 30, 2004 Messages 46 Location SE Yes I do...Thanks! /D_S Upvote 0 Downvote