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;

In reply to Parsing a .gpx file with Geo::Gpx by paulmorrow

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.