On Jun 14, 8:03 am, [EMAIL PROTECTED] (Jenda Krynicky) wrote:
> Date sent:              Thu, 14 Jun 2007 06:29:56 -0400
> From:                   Mathew Snyder <[EMAIL PROTECTED]>
> To:                     Perl Beginners <[EMAIL PROTECTED]>
> Subject:                Hash Key is a Null Value
>
> > I'm building a hash using values from a database backend to an application 
> > we
> > use in house.  The application has a field which contains a customer name.  
> > This
> > values is supposed to be set by the person handling the work but sometimes
> > doesn't get done.  This leaves a NULL value in the database which, in turn,
> > leaves me with a null key in my hash.
>
> > I've tried resetting it by assigning the value to another key so I can 
> > delete
> > the element but so far nothing has worked.  I've tried to access it with
> > $hash{}, $hash{""}, and $hash{''}.  None of these will allow me to access 
> > the data.
>
>   $hash{undef()}
>
> You need to use the () because otherwise Perl would automatically
> quote the undef. So $hash{undef} is equivalent to $hash{'undef'}.

Hash keys are strings.  Anything that's not a string gets
"stringified" and that stringified value is used as the key.  You can
not store undef, a reference, or even a number as a key to a hash.
You can store only their stringified versions.

The stringified version of undef is the empty string.  Therefore,
$hash{undef()} is exactly identical to $hash{""}.  Observe:

$ perl -MData::Dumper -le'
my %h;
$h{undef()} = 1;
$h{undef} = 2;
$h{""} = 3;
print Dumper(\%h);
'
$VAR1 = {
          '' => 3,
          'undef' => 2
        };

$ perl -MData::Dumper -le'
my %h;
$h{undef()} = 1;
$h{undef} = 2;
#$h{""} = 3;
print Dumper(\%h);
'
$VAR1 = {
          '' => 1,
          'undef' => 2
        };


Paul Lalli


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to