Well if we are going to this way, I would use 'exists' for the hash key and 'defined' for the array element, a personal preference.
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my %a;
if ( exists $a{'abd'}
and defined $a{'abd'}->[0]
)
{ print "yes\n"}
print Dumper \%a;
__END__
Prints:
$VAR1 = {};
I prefer the arrow notation instead of
$a{abd}[0]
However,
Update: not the same! forgot about 0, zero, Ooops
if ( $a{'abd'} and $a{'abd'}->[0])
{ print "yes\n"}
is the same.
Whether each term doesn't "exist" or is not "defined" has the same true/false meaning. To prevent the autovivification, each "level" of the hash has to be tested, starting from the first. As long as that is done, it doesn't matter whether 'exists' or 'defined' is tested for subsequent levels.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.