I’ve got an XML file, When I do a print Dumper on my now $data->{Foo}, I get the following output.
$VAR1 = [
{
'Bar' => {
...etc...
}
},
{
'Bar' => {
...etc2...
}
}
];
How do I print what’s under the second Bar? I tried:
$data->{Foo}{1}->{Bar}
But that’s incorrect syntax.
Thanks,
Dan
You’re going to get in trouble if you leave out the first ‘->’.
If you say
$foo->[0]Perl thinks thatfoois a scalar that’s a reference to an array, and then returns the first element of that referenced array.If you say
$foo[0]Perl thinks thatfoois an array, and returns it’s first element.You also need to be careful about
[]vs.{}.[]are for array lookups,{}are for hash lookups. Perl can convince an array that it’s a hash if it really wants to, with surprising results sometimes.So, given all that, you need to say something like this:
$data->{Foo}[1]{Bar};or more pedantically:
$data->{Foo}->[1]->{Bar};Given the comments below, the first form is preferred for what I think are pretty obvious reasons. See ‘Using References’ in
perldoc perlreffor more details.