XML の解析時に何か問題が発生しています。どういうわけか、2 つのループを繰り返す必要がありますが、必要なのは 1 つだけです。ここで何が間違っていますか?
最初に xml データ:
<fishy>
<fish displayName = "Scalar"
description = "the first fish I bought."
/>
<fish displayName = "crystal red"
description = "not really a fish."
/>
</fishy>
それから私のperlコード:
#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;
#Read configuration
my $simple = XML::Simple->new();
my $data = $simple->XMLin('test.xml');
print Dumper($data) . "\n";
my @fishes = $data->{fish};
#This is weird.. Why do we have nested arrays?
foreach my $fish (@fishes)
{
foreach my $innerFish (@{$fish})
{
print ("\n" . $innerFish->{displayName} . " is " . $innerFish->{description});
}
print ("\n");
}
そして最後に応答(これで問題ありません)
$VAR1 = {
'fish' => [
{
'description' => 'the first fish I bought.',
'displayName' => 'Scalar'
},
{
'description' => 'not really a fish.',
'displayName' => 'crystal red'
}
]
};
Scalar is the first fish I bought.
crystal red is not really a fish.