Dear monks,
I would like to describe my problem with some sample codes below.
my %Hash;
$Hash{'2012'}{'Jan'}{'13th'} = 1; # set 13th Jan 2012 to true
$Hash{'2012'}{'Feb'}{'10th'} = 1; # set 10th Feb 2012 to true
if ($Hash{'2013'}{'Oct'}{'11th'}{'1AM'}{'30Min'}) {
# if so, do something
}
## The if statement sets $Hash{'2013'}{'Oct'}{'11th'} to a hash ref
## which is undesirable.
## What I want is not to create $Hash{'2013'} at all.
## Later on if I check
if ($Hash{'2013'}{'Oct'}{'11th'}) {
## I will end up here . . .
## I only want to be here if I set
## $Hash{'2013'}{'Oct'}{'11th'} = 1 explicitly.
}
The problem is with the 'if ($Hash{'2013'}{'Oct'}{'11th'}{'1AM'}{'30Min'})' statement which creates $Hash{'2013'}{'Oct'}{'11th'}{'1AM'} as a hash ref. Is there any way to check the existence of a nested hash key without creating the parent hash keys? I can change the if statement to the one below but it looks clumsy . . .
if ($Hash{'2013'} && $Hash{'2013'}{'Oct'} && $Hash{'2013'}{'Oct'}{'11t
+h'} && $Hash{'2013'}{'Oct'}{'11th'}{'1AM'} && $Hash{'2013'}{'Oct'}{'1
+1th'}{'1AM'}{'30Min'}) {
# do something here
}