Simple question:
use warnings;
use XML::Simple;
use Data::Dumper;
my $file='test.osm';
my $test_data = XMLin( $file );
print Dumper( $test_data );
my @way_node_refs=$test_data->{'way'}->{'nd'};
print Dumper( @way_node_refs );
print( @way_node_refs[1]->{'ref'} );ere
It has the following output. The first dump is not given because it does not matter.
$VAR1 = [
{
'ref' => '453966480'
},
{
'ref' => '453966490'
},
{
'ref' => '453966482'
},
{
'ref' => '453966130'
},
{
'ref' => '453966143'
},
{
'ref' => '453966480'
}
];
How can I access the values with the key ref. I don't know how to interprete the braces kinda.
You have an array reference where the values inside the array are hash references. To access single values, use the arrow operator ->
.
print $foo->[0]->{ref};
print $foo->[1]->{ref};
Or you can loop through them.
foreach my $elem ( @{ $foo } ) {
print Dumper $elem;
print $elem->{ref};
}
You could even sort
them alphabetically by the values of the ref keys.
my @sorted = sort { $a->{ref} cmp $b->{ref} } @$foo; # note @sorted is an array
See perlref, perlreftut and perldsc for more details on data structures and references.