Thanks Troy and Bob. Your explanation makes perfect sense. I usually
dont code with perl -w, although I know I should and in this case it
would have saved me some trouble.

Bob Showalter wrote:

> > -----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]

--
Mark Maunder
Senior Architect
SwiftCamel Software
http://www.swiftcamel.com
mailto:[EMAIL PROTECTED]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to