I don't know if you realize it but you are modifying the input string so that
my $pos = latlong_cleanup($data);
# now $pos eq $data
You can grab the data you want using a single regex. I don't know what format the data is but I'll assume
23°34'N 12°25'W
use strict;
use warnings;
use YAML;
use utf8; # to cope with the degree symbol in my test data.
my $data = "23°34'N 12°25'W";
print Dump latlong_cleanup($data);
sub latlong_cleanup {
my $input = shift;
if ( my ( $degN, $minN, $NS, $degE, $minE, $EW )
= $input =~ /(\d+)\xB0(\d+)'([NS]) +(\d+)\xB0(\d+)'([EW])/ )
{
my $lat = sprintf "%0.2f",
( $NS eq 'S' ? -1 : 1 ) * ( $degN + ( $minN / 60 ) );
my $long = sprintf "%0.2f",
( $EW eq 'W' ? -1 : 1 ) * ( $degE + ( $minE / 60 ) );
return [ $lat, $long ];
}
die "Invalid data [$input]";
}
__END__
---
- 23.57
- -12.42
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|