Re: How to check empty hash value properly?



On Mon, 2008-10-06 at 22:35 +0900, Raymond Wan wrote:
Try:

if (defined $hash{$key}) {
...


Defined is not the same as exists.

#!/usr/bin/perl

use strict;
use warnings;

my %data = (
b => undef,
c => 1,
);

for my $key ( qw( a b c ) ){
print "\$key = $key\n";
if( exists $data{$key} ){
print "\t\$data{$key} exists\n";
if( defined $data{$key} ){
print "\t\$data{$key} defined\n";
print "\t\$data{$key} = $data{$key}\n";
}else{
print "\t\$data{$key} not defined\n";
}
}else{
print "\t\$data{$key} not exists\n";
}
print "\n";
}

__END__



--
Just my 0.00000002 million dollars worth,
Shawn

Linux is obsolete.
-- Andrew Tanenbaum

.



Relevant Pages