Thank you Monks, for your great help. I did try NetWallah's code,thanks O great Abbot. I am however getting some errors originating from the subroutine which am sharing here as you had suggested. I previously got such errors while trying different loops but avoided tinkering with the subroutine which works as it is. What should i change in the new code? Here are the exceptions am getting and the subroutine code;

The errors, the line 5852 is the last line in my CSV file
C:\MyScripts\2G3Gdistance.pl
Argument "Longitude" isn't numeric in subtraction (-) at
C:\MyScripts\2G3Gdistance.pl line 53, <COMBINED> line 5852 (#1)
Argument "Latitude" isn't numeric in multiplication (*) at
C:\MyScripts\2G3Gdistance.pl line 80, <COMBINED> line 5852 (#1)
Use of uninitialized value $lon2 in subtraction (-) at
C:\MyScripts\2G3Gdistance.pl line 53, <COMBINED> line 5852 (#2)
(W uninitialized) An undefined value was used as if it were already
defined. It was interpreted as a "" or a 0, but maybe it was a mistake.
To suppress this warning assign a defined value to your variables.
Use of uninitialized value $deg in multiplication (*) at
C:\MyScripts\2G3Gdistance.pl line 80, <COMBINED> line 5852 (#2)
Uncaught exception from user code:
Can't take sqrt of -4.44089e-016 at C:\MyScripts\2G3Gdistance.pl line 71, <COMBINED> line 5852.
main::acos(1) called at C:\MyScripts\2G3Gdistance.pl line 55
main::distance(-0.433525648, 36.968599085, -0.433525648, 36.96859909, "K") called at C:\MyVolume\Development\MyScripts\2G3Gdistance.pl line 150

The subroutine code

my $pi = atan2(1,1) * 4; sub distance { my ($lat1, $lon1, $lat2, $lon2, $unit) = @_; my $theta = $lon1 - $lon2; my $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad +($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $dist = $dist * 60 * 1.1515; if ($unit eq "K") { $dist = $dist * 1.609344; } elsif ($unit eq "N") { $dist = $dist * 0.8684; } return ($dist); } #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +: #::: This function get the arccos function using arctan function :: +: #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +: sub acos { my ($rad) = @_; my $ret = atan2(sqrt(1 - $rad**2), $rad); return $ret; } #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +: #::: This function converts decimal degrees to radians :: +: #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +: sub deg2rad { my ($deg) = @_; return ($deg * $pi / 180); } #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +: #::: This function converts radians to decimal degrees :: +: #::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: +: sub rad2deg { my ($rad) = @_; return ($rad * 180 / $pi); } print distance(32.9697, -96.80322, 29.46786, -98.53506, "K") . " Kilom +eters\n";

In reply to Re^2: Nested foreach loops by keienn
in thread Nested foreach loops by keienn

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.