我創建一個XML傳遞給一個API和API返回這個(轉儲數據):在perl中我如何循環這個結構?
(
"Data::Dump",
{
SiteDevices => {
"device" => {
1102 => { address => "1.2.3.4", riskfactor => "1.0", riskscore => "0.0" },
1136 => { address => "1.2.3.5", riskfactor => "1.0", riskscore => "0.0" },
20491 => { address => "1.2.3.6", riskfactor => "1.0", riskscore => "0.0" },
129644 => { address => "1.2.3.7", riskfactor => "1.0", riskscore => "0.0" },
129645 => { address => "1.2.3.8", riskfactor => "1.0", riskscore => "0.0" },
130408 => { address => "1.2.3.9", riskfactor => "1.0", riskscore => "0.0" },
135975 => { address => "1.2.3.10", riskfactor => "1.0", riskscore => "0.0" },
137642 => { address => "1.2.3.11", riskfactor => "1.0", riskscore => "0.0" },
},
"site-id" => 27,
},
success => 1,
},
)
我通過要循環和打印他們和我有關的設備和IP爲我的生活不能拿出任何代碼來做到這一點。我錯過了什麼?!我試圖循環散列,哈希散列等。永遠不能得到它的工作。如果你們中的任何一個人有第二個,並且能夠提供答案,那麼我就可以搖頭,這真是太棒了。
我曾嘗試:
foreach my $key (keys %{ $output->{‘SiteDevices’}->{‘device’} }) {
print $key
print $key->{‘address’}
}
和
foreach my $key (keys %{ $output{‘SiteDevices’}{‘device’} }) {
print $key
print $key{‘address’}
}
但既不工程。
輸出中有多少'SiteDevices'條目?只有一個? – stevieb
這看起來像解析XML一樣可疑。有沒有機會?如果是這樣,我建議你最好在上游工作並使用更好的解析器。添加一個XML的例子,給你一個更好的例子會容易得多。 – Sobrique