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
    Most probably the $gpx object variable was not initialized correctly. Probably the gpx-file was not read correctly. Add an "or die "Error in opening gpx file: $!; to line 6 and run the script again.

    Update: deleted a spurious double quote.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

      Count Zero, thanks for your reply, I inserted your text one space after the last word in line 6:

      "or die "Error is opening gpx file: $!;

      as you had instructed, with no luck, the error is below. I also tried some different combinations of positioning the " quotation marks and leaving them out all together.

      paul@intern-PM:~/Desktop/San Diego$ perl gpxscript2 String found where operator expected at gpxscript2 line 6, near "'sandiegopark.gpx' "or die "" (Missing operator before "or die "?) Bareword found where operator expected at gpxscript2 line 6, near ""or die "Error" (Missing operator before Error?) syntax error at gpxscript2 line 6, near "'sandiegopark.gpx' "or die "" Global symbol "$fh_in" requires explicit package name at gpxscript2 line 8. Execution of gpxscript2 aborted due to compilation errors.

      with no quotes I receive the message: paul@intern-PM:~/Desktop/San Diego$ perl gpxscript2 syntax error at gpxscript2 line 6, near "file:" Global symbol "$fh_in" requires explicit package name at gpxscript2 line 8. Execution of gpxscript2 aborted due to compilation errors.

      Sorry, I'm not sure how to proceed from here. I was also looking at a XML parser, for you can do a save as .xml, do you suggest something like that?

        That line should read:
        open my $fh_in, '<', 'fells_loop.gpx' or die "Error in opening gpx fil +e: $!";
        Try it and see what error you get.

        CountZero

        A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        My blog: Imperial Deltronics
        I'm not sure what the code looks like when you have it "without quotes", so to remove any confusion, could you revert to the script as it was without out the "or die" stuff and just add use autodie; to the top? What, if any, errors do you get now? Thanks.