None of the solutions do what you asked--although they may do what you want. That's because a key can exist, yet its corresponding value is undef:
use strict;
use warnings;
use 5.010;
my %hash = (
'one' => undef,
);
say "The key 'one' exists" if exists $hash{one};
say "The key 'two' exists" if exists $hash{two};
--output:--
The key 'one' exists
Note that nothing prints out for the non-existent key: 'two'. Then if you do this:
$hash{one} ||= 1;
say $hash{one};
--output:--
1
you will assign 1 to a key that exists--which does not meet your stated requirement.
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.