> -----Original Message-----
> From: Mark Maunder [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, August 22, 2001 1:40 PM
> To: [EMAIL PROTECTED]
> Subject: Why does %hdata = undef; not work as expected
>
>
> This is probably an oldie, but assigning an undef to a hash
> creates a hash
> with a single element (which I'm guessing is undef). Isn't
> this counter
> intuitive. I would have expected the hash to be empty if the
> undef that is
> assigned is in scalar context.
Enabling warnings and diagnostics helps explain what's
going on here.
The left side of an assignment determines the context. So
we have a list assignment. undef returns undef in list or
scalar context.
So the assignment is trying to take key/value pairs from the
list on the right side. The "list" contains only one value,
undef, which becomes the "key" part of the key/value pair.
Since no "value" part is given, an undef is supplied as the
value.
The assignment thus becomes effectively:
%hdata = (undef => undef);
But hash keys must be strings, so the first undef is converted
to a string, ''. The resulting hash is thus:
'' => undef;
The way to empty a hash is
undef %h;
or
%h = ();
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]