我正在尝试做这样的事情:
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;
}
本质上,我在解析XML时得到哈希散列的哈希值。我想使用此配置哈希%fields_to_check来访问此哈希结构中的这些不同值.Essential $ key是一串键,用于指向我想去的地方。有人知道这是可能的还是知道另一种解决方案?
答案 0 :(得分:0)
这是一个粗略的想法:
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));