paulmorrow has asked for the wisdom of the Perl Monks concerning the following question:
I am attempting to parse a .gpx document and referencing the script below (thanks to Simbabque), the current error I am receiving is:
Can't use an undefined value as an ARRAY reference at gpxscript2 line 19.
I have just begun reading about arrays in my Beginning Perl book and am stuck. I read that the array must start with an alpha character or an underscore. I attempted to label the array and reconfigure this line with no luck... Any suggestions would be appreciated. I am using Ubuntu 12.04, and installed the Geo::Gpx,DateTime::Format::ISO8601, & XML::Descent successfully
Here is the script: Thanks!
use strict; use warnings; use Geo::Gpx; use DateTime; # Open the GPX file open my $fh_in, '<', 'fells_loop.gpx'; # Parse GPX my $gpx = Geo::Gpx->new( input => $fh_in ); # Close the GPX file close $fh_in; # Open an output file open my $fh_out, '>', 'fells_loop.csv'; # Print the header line to the file print $fh_out "time,lat,lon,ele,name,sym,type,desc\n"; # The waypoints-method of the GEO::GPX-Object returns an array-ref # which we can iterate in a foreach loop foreach my $wp ( @{ $gpx->waypoints() } ) { # Some fields seem to be optional so they are missing in the hash. # We have to add an empty string by iterating over all the possible # hash keys to put '' in them. Map is like a foreach loop. map { $wp->{$_} ||= '' } qw( time lat lon ele name sym type desc ); # The time is a unix timestamp, which is hard to read. # We can make it an ISO8601 date with the DateTime module. # We only do it if there already is a time, though. if ($wp->{'time'}) { $wp->{'time'} = DateTime->from_epoch( epoch => $wp->{'time'} ) ->iso8601(); } # Join the fields with a comma and print them to the output file print $fh_out join(',', ( $wp->{'time'}, $wp->{'lat'}, $wp->{'lon'}, $wp->{'ele'}, $wp->{'name'}, $wp->{'sym'}, $wp->{'type'}, $wp->{'desc'}, )), "\n"; # Add a newline at the end } # Close the output file close $fh_out;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parsing a .gpx file with Geo::Gpx
by CountZero (Bishop) on Jun 27, 2012 at 21:16 UTC | |
by paulmorrow (Initiate) on Jun 27, 2012 at 22:05 UTC | |
by CountZero (Bishop) on Jun 28, 2012 at 06:36 UTC | |
by frozenwithjoy (Priest) on Jun 27, 2012 at 23:34 UTC |