Got some problems to access json values (translated from xml).
for my $PORT (0..$#PORTS)
{
print Dumper $PORTS[$PORT]->{'neighbor'};
if (defined($PORTS[$PORT]->{'neighbor'}->{'wwn'}->{'$t'}))
{
print Dumper @{$PORTS[$PORT]->{'neighbor'}->{'wwn'}->{$t}};
}
else
{
print Dumper "not defined"
}
}
Test works when $PORTS[$PORT]->{'neighbor'}
is empty but I’ve an error when it contains some value.
$VAR1 = {};
$VAR1 = 'not defined';
$VAR1 = {};
$VAR1 = 'not defined';
$VAR1 = {
'wwn' => [
{
'$t' => 'xxx'
},
{
'$t' => 'yyy'
},
{
'$t' => 'zzz'
}
]
};
Not a HASH reference at ./xxxxx.pl line 209.
Line 209 is the line of the test :
if (defined($PORTS[$PORT]->{'neighbor'}->{'wwn'}->{'$t'}))
Don’t know what I miss !
Some clues ?
2
Answers
Finally solved by checking type returned :
Thanks to all...
Two errors.
In the undefined case, you check if
$PORTS[$PORT]{neighbor}{wwn}{'$t'}
is defined as if$PORTS[$PORT]{neighbor}{wwn}
is a reference to a hash, but it’s$PORTS[$PORT]{neighbor}{wwn}
that’s not defined.In the defined case, you use
$PORTS[$PORT]{neighbor}{wwn}
as a reference to a hash when it’s actually a reference to an array.