Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm coding some logging configuration info into hash of hashes - for instance:
my %Log_names = ( "BM_therm_boiler_00_state" => { 'events_log' => 1 }, "BM_therm_house_00_state" => { 'events_log' => 1 }, "BM_therm_hpump_00_state" => { 'events_log' => 1 }, "BM_swi_" => { 'device_log' => 1 }, );


Now I'd like to check for certain key whether it exists and retrieve it's content. But using simple exists or defined with double hashes, like
if exists $Log_names{"BM_light_test_00_"}{"device_log"}
I get the right result but it seems that keys are added to hash. Can I prevent this ?

$VAR1 = { 'BM_light_test_00_' => {}, 'BM_swi_' => { 'device_log' => 1 }, 'BM_therm_house_00_state' => { 'events_log' => 1 }, 'BM_therm_boiler_00_state' => { 'events_log' => 1 }, 'BM_therm_hpump_00_state' => { 'events_log' => 1 } };
How to properly check existence and retrieve its content from hash of hashes ?

Thanks in advance,

regards,

Rob.

Replies are listed 'Best First'.
Re: Checking existence in hash of hashes without adding entries
by ccn (Vicar) on Dec 19, 2008 at 21:20 UTC
    if exists $Log_names{"BM_light_test_00_"} and exists $Log_names{"BM_light_test_00_"}{"device_log"}
Re: Checking existence in hash of hashes without adding entries
by holli (Abbot) on Dec 19, 2008 at 22:13 UTC
Re: Checking existence in hash of hashes without adding entries
by Your Mother (Archbishop) on Dec 19, 2008 at 22:29 UTC
Re: Checking existence in hash of hashes without adding entries
by lakshmananindia (Chaplain) on Dec 20, 2008 at 03:23 UTC

    Here is the another way using ref

    if(ref $Log_names{BM_light_test_00_} eq 'HASH' && exists($Log_names{BM +_light_test_00_}{device_log})) { print "Exists\n"; }