下面的代码是我从XML::Simple
获得的XML结构的一部分。
我有很多classfield
(classfield_101
,classfield_1023
,...),classfield_101
是其中之一。对于classfield_101
,我希望程序将'CHECK UNCHECK'
打印到控制台。
my (%ClassDetails) = ();
my %struct = (
'level1' => {
'classfield' => {
'classfield_101' => {
'classfieldvalues' => {
'classfieldvalue' => [
{'content' => 'CHECK', 'key' => '1254789'},
{'content' => 'UNCHECK', 'key' => '1987456'}
]
},
'classfieldname' => 'CHECKCLASS',
'key' => 'CHECKDETAILS'
},
}
},
);
print Dumper(\%struct);
for my $elem (@{$struct{level1}{classfield}{classfield_101}{classfieldvalues}{classfieldvalue}}) {
print "$elem->{content} ";
}
#Output
#CHECK UNCHECK
如何遍历classfield_id
中的每个%$classfield
并打印值?
foreach $classfield_id (keys %$classfield) {
## for ex: Looping for classfield_id = classfield_101 from %$classfield
if ( $classfield->{$classfield_id}->classfieldname eq "CHECKCLASS" ) {
### Stuck here --- $ClassDetails{checkuncheck} = How to print
}
}
答案 0 :(得分:-1)
下面将打印您的整个哈希。您可以修改代码以打印您感兴趣的任何内容:
foreach my $key (keys %struct) {
print "$key\n";
foreach my $key2 (keys %{$struct{$key}}) {
print "-$key2\n";
foreach my $key3 (keys %{$struct{$key}->{$key2}}) {
print "--$key3\n";
foreach my $key4 (keys %{$struct{$key}->{$key2}->{$key3}}) {
my $ref = ref $struct{$key}->{$key2}->{$key3}->{$key4};
if($ref eq '') {
print "---$key4 = $struct{$key}->{$key2}->{$key3}->{$key4}\n";
}
else {
print "---$key4\n";
foreach my $key5 (keys %{$struct{$key}->{$key2}->{$key3}->{$key4}}) {
print "----$key5\n";
foreach my $key6 (@{$struct{$key}->{$key2}->{$key3}->{$key4}->{$key5}}) {
foreach my $key7 (keys %{$key6}) {
print "-----$key7 = $key6->{$key7}\n";
}
}
}
}
}
}
}
}