Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Curious about Hashes which viewed as a list has only one item

by ack (Deacon)
on Feb 28, 2008 at 23:56 UTC ( #671055=perlquestion: print w/replies, xml ) Need Help??
ack has asked for the wisdom of the Perl Monks concerning the following question:

This regards a thread started in Meditations to which I wanted to reply (actually ask a question); but I thought it more appropriate to bring my question into this forum. The original node in Meditations involved a simple construct from some legacy code the author was trying to maintain. It included the following code snippet:

%foo ={}; unlink %foo;

I don't believe the subsequent discussion is relevant (the reference is from this post by vrk) nor am I particularly concerned about the unlink portion (though that was what most of the rest of the thread was about).

But my question is as follows:

Since the original hash, %foo, was assigned an anonymous hash, doesn't that make it a hash with a single entry (that entry is a reference to the anonymous hash)? So %foo's entry is a single value and it resolves to the stringified version of the anonymous hash (if it is, indeed, used as a key)...doesn't it?

But, presuming that %foo (viewed as a list) only has the single entry (the author of the node says on his machine the stringified reference is HASH(0x814dc30)), then it begs...for me, at least...another question. How does a hash handle a single value...i.e., it either has no key or no value for the key. That is to say, is the HASH(0x814dc30) a key or a value?

I naively presume that it is considered the key (the stringified version of the anonomyous hash reference) and that the value associated with the key is undef (or maybe it is just empty, a null?).

So, my question is how does Perl handle a hash like the above if that hash is used in a list context (which is important to understanding what unlink will do given the hash, unlink will use the hash in its list context)?

ack
Albuquerque, NM

Replies are listed 'Best First'.
Re: Curious about Hashes which viewed as a list has only one item
by BrowserUk (Pope) on Feb 29, 2008 at 00:02 UTC
      Actually, that would be 'elicited', although in this context, I like the way you're thinking, that would be a good word if it existed ;)

      Thanks, BrowserUk. That was what I suspected would be the case...just wasn't sure if it would be undef or something else.

      Your note on the complaining message from Perl had the strict warnings been in effect also tells me what I had hoped would be the case...it recognizes that there aren't the even number of entries in the list to allow it to be a 'normal hash'. Errto also noted a similar expectation (though a different message).

      Thanks everyone. That really relieves my curiosity.

      ack Albuquerque, NM
Re: Curious about Hashes which viewed as a list has only one item
by Errto (Vicar) on Feb 29, 2008 at 00:03 UTC

    It's a key, with the undefined value as its corresponding value. If you have warnings enabled you'll get a warning "Odd number of elements in hash assignment"

    If you use that hash in list context you'll get a list of a single element which contains that string HASH... By the way, note that it is a string, it's not an actual reference. The reason is that in Perl, hash keys are always treated as strings; other scalars (numbers, references, undef) are stringified when you use them as a hash key.

      Thanks, errto. That was what I suspected would be the case. So do I understand correctly that if I were to do something like:

      print "yep" if(defined $foo{HASH(xxx)});

      then I'd not expect any print? (The xxx is the stringified octal code in the pre-stringified reference)

      Also, thanks for the note on the stringified reference. I remember well (from having made the erroneous assumption quite a few times) that the key is just the stringified version of the reference and is not, itself, a referene.

      Again, thanks for the info...it really helped.

      ack Albuquerque, NM
        Well, for one thing if you were actually writing that literally you'd have to put the HASH(xxx) bit in quotes. But you're correct, that statement would not print anything. But if you changed defined to exists, then it would print yep because that key is really present in the hash.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://671055]
Approved by Errto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2017-10-19 21:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (257 votes). Check out past polls.

    Notices?