Question

I'm trying to do something like this:

my $xml_hash_ref = XML::Parser......


my %fields_to_check = (
            '{Key1}{Key2}{Key3}{Key4}' => '..another hash...'
            '{Key1}{DifferentKey2}'    => '...another hash...'
            '{Key1}{DifferentKey2}{DifferentKey3}'    =>  '...another hash...'
);

foreach my $key (keys %fields_to_check){
     my $value = $xml_hash_ref->$key;
}

essentially I get this large hash of hashes of hashes when parsing an XML. I want to access these different values in this hash structure using this config hash %fields_to_check.Essential $key is a string of keys to direct where I want to go. Anyone know if this is possible or know of another solution?

Était-ce utile?

La solution

Here's a rough idea:

use strict;
use warnings;

my $deep_hash = {
    aa => {
        bb => { cc => 111, dd => 222 },
        ee => 333,
    },
    ff => 444,
};

sub dive {
    my $h = shift;
    for my $k (@_){
        return unless (ref($h) eq 'HASH' and exists $h->{$k});
        $h = $h->{$k};
    }
    return $h;
}

dive($deep_hash, qw(aa bb dd));
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top