Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by Errto (Vicar)
on Feb 29, 2008 at 00:03 UTC ( #671057=note: print w/replies, xml ) Need Help??


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

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.

  • Comment on Re: Curious about Hashes which viewed as a list has only one item

Replies are listed 'Best First'.
Re^2: Curious about Hashes which viewed as a list has only one item
by ack (Deacon) on Feb 29, 2008 at 05:28 UTC

    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: note [id://671057]
help
Chatterbox?
[GotToBTru]: 99% of all deaths take place within 24 hours of ingesting di-hydrogen monoxide
[GotToBTru]: time for some C8H10N4O2 for me
[ambrus]: GotToBTru: wait, you tell only the atom totals of what you want? Is that like ordering food in a restaurant by telling only the nutrient amounts you need, or
[ambrus]: like when a medieval scientist supposedly proves his priority inventing something by having previously published an anagram of a thousand letters long summary of the invention?

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2016-12-06 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (104 votes). Check out past polls.