YouHaveaBigEgo
YouHaveaBigEgo

Reputation: 220

How can I look and search for a key inside a heavily nested hash?

I am trying to check if a BIG hash has any keys from small hash and see if they exist, and if they do modify the BigHash with updated values from small hash.

So the lookup hash would look like this :

configure =(
    CommonParameter => {
        'SibSendOverride' => 'true',
        'SibOverrideEnabledFlag' => 'true',
        'SiPosition' => '8',
        'Period' => '11'
    }
)

But the BigHash is very very nested.. The key/hash CommonParameter from the small hash configure is there in the BigHash.

Can somebody help/suggest some ideas for me please?

Here is an example BigHash :

%BigHash = (

'SibConfig' => {
                'CELL' => {
                            'Sib9' => {
                                        'HnbName' => 'HnbName',
                                        'CommonParameter' => {
                                                                'SibSendOverride' => 'false',
                                                                'SibMaskOverrideEnabledFlag' => 'false',
                                                                'SiPosition' => '0',
                                                                'Period' => '8'
                                                         }
                                 }
                          }
                },
)

I hope I was clear in my question. Trying to modify values of heavily nested BigHash based on Lookup Hash if those keys exist.

Can somebody help me? I am not approaching this in the right way. Is there a neat little key lookup fucntion or something available perhaps?

Upvotes: 1

Views: 74

Answers (1)

mob
mob

Reputation: 118615

Give Data::Search a try.

use Data::Search;
@results = Data::Search::datasearch(
   data => $BigHash, search => 'keys',
   find => 'CommonParameter',
   return => 'hashcontainer');

foreach $result (@results) {
    # result is a hashref that has 'CommonParameter' as a key
    if ($result->{CommonParameter}{AnotherKey} ne $AnotherValue) {
        print STDERR "AnotherKey was ", $result->{CommonParameter}{AnotherKey},
             " ... fixing\n";
        $result->{CommonParameter}{AnotherKey} = $AnotherValue;
    }
}

Upvotes: 2

Related Questions