I've written some code which calculates how far an animal is from a set location. I want to be able to return the number of animals who are within a certain distance (10km) from the set location.
Im using a data file with 300+ animals sorted into Name - Latitude - Longitude.
I thought I would create a counter for this, which would increment by 1 when the program found an animal which was within the 10km, but I'm not having any luck with that. (E.g. along the lines of "if $Dist < 10, ++counter"
Here's my code so far:
$Mam = 'Mammal.txt';
open(FILE, $Mam) or die "Error. Could not open '$Mam'.";
while(<FILE>)
{
chomp;
@Loc = split(/\t/, $_);
my $Dist;
$Dist = CalculateDistance(54.988056, -1.619444, $Loc[1], $Loc[2]);
The formatting is terrible at the moment since I'm just playing with it. I havn't included the CalculateDistance subroutine as it is quite long, but I can if it will help.
I tried
while($Dist < 10)
{
$counter++;
print "$counter animals are found withing 10KM\n"
}
but just get the number 0 repeated. The code should be displaying one number which represents the total number of animals found within 10KM of the coordinates provided. I can't see where I'm going wrong.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.