使用data:dumper,XML输出显示如下。
$VAR1 = {
'af' => {
'minimum' => {
'requested_bytes' => '8208'
},
'time' => [
{
'lastthreadtid' => '0x0000000030001100',
'exclusiveaccessms' => '0.017',
'meanexclusiveaccessms' => '0.017',
'threads' => '0'
},
{
'totalms' => '41.829'
}
],
'tenured' => [
{
'loa' => {
'freebytes' => '1314816',
'totalbytes' => '1314816',
'percent' => '100'
},
'freebytes' => '1314816',
'totalbytes' => '69219328',
'percent' => '1',
'soa' => {
'freebytes' => '0',
'totalbytes' => '67904512',
'percent' => '0'
}
},
{
'loa' => {
'freebytes' => '628664',
'totalbytes' => '685056',
'percent' => '91'
},
'freebytes' => '41796952',
'totalbytes' => '68537344',
'percent' => '60',
'soa' => {
'freebytes' => '41168288',
'totalbytes' => '67852288',
'percent' => '60'
}
}
],
'timestamp' => 'May 01 20:33:51 2016',
'intervalms' => '681.342',
'refs' => [
{
'weak' => '19116',
'maxSoftReferenceThreshold' => '32',
'phantom' => '0',
'dynamicSoftReferenceThreshold' => '20',
'soft' => '1709'
},
{
'weak' => '18490',
'maxSoftReferenceThreshold' => '32',
'phantom' => '0',
'dynamicSoftReferenceThreshold' => '19',
'soft' => '1709'
}
],
'gc' => {
'classunloading' => {
'classes' => '0',
'timevmquiescems' => '0.000',
'timetakenms' => '1.418',
'classloaders' => '0'
},
'finalization' => {
'objectsqueued' => '8'
},
'timesms' => {
'compact' => '0.000',
'mark' => '39.600',
'sweep' => '0.495',
'total' => '41.787'
},
'tenured' => {
'loa' => {
'freebytes' => '628664',
'totalbytes' => '685056',
'percent' => '91'
},
'freebytes' => '41805160',
'totalbytes' => '68537344',
'percent' => '60',
'soa' => {
'freebytes' => '41176496',
'totalbytes' => '67852288',
'percent' => '60'
}
},
'intervalms' => '681.373',
'type' => 'global',
'id' => '24',
'totalid' => '24',
'contraction' => {
'timetaken' => '0.000',
'amount' => '681984',
'newsize' => '68537344',
'reason' => 'excess free space following gc',
'type' => 'tenured'
}
},
'type' => 'tenured',
'id' => '24',
'pending-finalizers' => [
{
'reference' => '0',
'finalizable' => '8',
'classloader' => '0'
},
{
'reference' => '0',
'finalizable' => '8',
'classloader' => '0'
}
]
}
};
在perl代码中,写如下抛出数组错误。
print Dumper($dataXML);
print $dataXML->{af}->{type}.",";
print $dataXML->{af}->{id}.",";
print $dataXML->{af}->{timestamp}.",";
print $dataXML->{af}->{intervalms}.",";
print $dataXML->{af}->{minimum}->{requested_bytes}.",";
print $dataXML->{af}->{time}[1]->{totalms}.",";
print $dataXML->{af}->{tenured}[1]->{freebytes}.",";
print $dataXML->{af}->{tenured}[1]->{totalbytes}.",";
print $dataXML->{af}->{tenured}[1]->{percent}.",";
print $dataXML->{af}->{tenured}[1]->{loa}[1]->{freebytes}.",";
print $dataXML->{af}->{tenured}[1]->{loa}[1]->{totalbytes}.",";
print $dataXML->{af}->{tenured}[1]->{loa}[1]->{percent}.",";
print $dataXML->{af}->{tenured}[1]->{soa}[1]->{freebytes}.",";
print $dataXML->{af}->{tenured}[1]->{soa}[1]->{totalbytes}.",";
print $dataXML->{af}->{tenured}[1]->{soa}[1]->{percent}.",";
print $dataXML->{af}->{tenured}[1]->{freebytes}[2].",";
print $dataXML->{af}->{tenured}[1]->{totalbytes}[2].",";
print $dataXML->{af}->{tenured}[1]->{percent}[2].",";
print $dataXML->{af}->{tenured}[1]->{loa}[2]->{freebytes}.",";
print $dataXML->{af}->{tenured}[1]->{loa}[2]->{totalbytes}.",";
print $dataXML->{af}->{tenured}[1]->{loa}[2]->{percent}.",";
print $dataXML->{af}->{tenured}[1]->{soa}[2]->{freebytes}.",";
print $dataXML->{af}->{tenured}[1]->{soa}[2]->{totalbytes}.",";
print $dataXML->{af}->{tenured}[1]->{soa}[2]->{percent}.",";
错误:
Not an ARRAY reference
我尝试过多种方法来获取每个数组键值的值,但它只显示最后一个值。
答案 0 :(得分:1)
关于您的问题:出现某些错误的源代码行总是很有用,因为它有助于解决方案。对于引用代码,最好提一下哪一行触发了错误。
Perl知道两种常见的引用类型:Array和Hash。
数组是一个有序的项目列表。每个项目都由唯一的序列号引用。可以在开头,结尾或任何其他位置预先添加,附加,删除或替换项目。 (请参阅unshift
,push
,shift
,pop
,splice
- 所有这些都是可点击的链接)。数组通常使用[ ]
:$array[0]
解决第一项,->[ ]
取消引用数组引用。
哈希是键/值存储:每个项目都有一个键和一个值,其中两个都可以包含任何字符,并且可以是任意大小。项目是无序的 - 因为没有订单 - 不能预先添加或附加。可以通过为现有密钥分配新值或使用delete
删除它们来替换它们。 { }
用于访问哈希项,->{ }
用于取消引用哈希引用。
您的代码混合了两种解除引用样式:
print $dataXML->{af}->{time}[1]->{totalms}.",";
更好的写作:
print $dataXML->{af}->{time}->[1]->{totalms}.",";
(在时间和->
之间添加[1]
。){hashref}[array_index]
的所有后续事件都相同。对于读者来说,始终使用->
更为明显。
您的错误首先出现在此行中:
print $dataXML->{af}->{tenured}->[1]->{loa}->[1]->{freebytes}.",";
正如其他人已在您的问题的评论中概述的那样:键{loa}
的值不是数组引用,而是哈希引用。这就是为什么你不能使用->[ ]
取消引用(=访问引用的数据结构)。但删除该附加数组引用也会删除错误:
print $dataXML->{af}->{tenured}->[1]->{loa}->{freebytes}.",";
详细了解http://perldoc.perl.org/perlref.html
上的参考资料 PS:您始终访问数据结构中的第二个数组项。这可能是你想要的,但要记住,Perl数组通常是从零开始的:第一个元素是[0]
。