pglinx has asked for the wisdom of the Perl Monks concerning the following question:

Tried many a thing to get this short code to run but all I get it 0 distance for an answer odd because it totally seem like it should work.
#!/usr/local/bin/perl5 use Math::Trig qw(deg2rad pi great_circle_distance asin acos); ############################ ## Distances are in Miles ## ############################ $lat1 = 51.29006786; $long1 = -114.0034133; $lat2 = 54.431963; $long2 = -110.20734554; { ($lat1, $long1, $lat2, $long2) = @_; $r=3956; $a = (pi/2)- deg2rad($lat1); $b = (pi/2)- deg2rad($lat2); $c = sqrt($a**2 + $b**2 - 2 * $a *$b *cos(deg2rad($long2)-deg2rad($long1))); $dist = $c * $r; } print "$dist\n";
Thanks for any help in advance.

Replies are listed 'Best First'.
Re: flat earth distance
by ikegami (Patriarch) on Apr 29, 2009 at 00:05 UTC

    Yet another question that would have been prevented by the use use strict; and use warnings; . Or even by printing out the values of variables until you find one that doesn't contain the value it should.

    What do you think @_ contains?

Re: flat earth distance
by Anonymous Monk on Apr 29, 2009 at 00:05 UTC
    This line is wrong  ($lat1, $long1, $lat2, $long2) = @_; @_ outside a sub is empty. perlvar, perlsub
Re: flat earth distance
by bobf (Monsignor) on Apr 29, 2009 at 03:08 UTC
Re: flat earth distance
by swampyankee (Parson) on Apr 29, 2009 at 10:48 UTC

    Have you tried that Math::Trig function great_circle_distance? Even if your task was to demonstrate code to calculate great circle distance, it would let you compare your results to those from reliable code.


    Information about American English usage here and here. Floating point issues? Please read this before posting. — emc

Re: flat earth distance
by Anonymous Monk on Apr 29, 2009 at 00:06 UTC