sophate has asked for the wisdom of the Perl Monks concerning the following question:
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 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Undesirable parent hash keys
by davido (Cardinal) on Jan 11, 2013 at 07:58 UTC | |
by sophate (Beadle) on Jan 16, 2013 at 06:20 UTC | |
|
Re: Undesirable parent hash keys
by Athanasius (Archbishop) on Jan 11, 2013 at 07:50 UTC | |
by sophate (Beadle) on Jan 11, 2013 at 07:55 UTC | |
|
Re: Undesirable parent hash keys (concept?)
by LanX (Saint) on Jan 11, 2013 at 08:43 UTC | |
by sophate (Beadle) on Jan 11, 2013 at 09:26 UTC | |
by CountZero (Bishop) on Jan 11, 2013 at 10:25 UTC | |
by tobyink (Canon) on Jan 11, 2013 at 10:34 UTC | |
|
Re: Undesirable parent hash keys
by Anonymous Monk on Jan 11, 2013 at 08:10 UTC | |
|
Re: Undesirable parent hash keys
by Anonymous Monk on Jan 11, 2013 at 17:26 UTC |