First things first. ++ for writing a clear and detailed description of your question, including an explanation of why you need it and what your troubles are, without omitting to mention the things you have tried.

That being said, I must add that you should've known your question is based upon wrong assumptions. I won't hold that to you, though, since I'm having a rather hard time too to find the docs that say that autovivication doesn't happen when you simply check wether a hash key exists or is true.

Anyway, I do remember I read it somewhere and I wrote a small script to prove it. Here goes:

use strict; # or die use warnings; # or die my %hash = ( cogito => "ergo sum" # I think, so I exist ); if (exists $hash{absum}) { print "Absum exists."; } else { print "Absum doesn't exist.\n"; # which would explain it's name... } print "Keys in the hash: ", join(" :: ", keys %hash), "\n"; __END__ Absum doesn't exist. Keys in the hash: cogito

So there you have it. As for your second question, simply use a boolean OR.

use strict; use warnings; my %hash = ( list1 => ["foo", "bar", "baz"] ); print "list1: [- ", join(" :: ", @{ $hash{list1} || []}), " -]\n"; print "list2: [- ", join(" :: ", @{ $hash{list2} || []}), " -]\n"; __END__ list1: [- foo :: bar :: baz -] list2: [- -]

Admittedly, that  || [] part in there isn't quite a beauty either but it does what you need without too much typing.


In reply to Re: Best Hash Practices? by muba
in thread Best Hash Practices? by DamianKaelGreen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.