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

I've ben trying to convert a php script to work as perl and I must be missing somthing because all I am doing is trying to loop through a file and display listing that are under 30 miles away as in a store locator. It is easier to see what is going on then for me to explain, Any help is welcome.
#!/usr/local/bin/perl5 #------------------ was php--------------------------- funtion distance($lat1, $lon1, $lat2, $lon2, $unit) { $theta = $lon1 - $lon2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($la +t1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; $unit = strtoupper($unit); if ($unit == "K") { return ($miles * 1.609344); } elsif ($unit == "N") { return ($miles * 0.8684); } else { return $miles; } } #--------------------------end was php------------------------ { $selectmiles = 30; $zipcood = "60.1843160, -128.5069940"; open(FILE, "area.txt") || die "I can't open that because: $!\n"; while(<FILE>) { chop; @all = split(/\n/); foreach $line (@all) { local($hStoreID, $hStoreName, $hStoreUserName, $hStorePassword, $h +StoreMCRSPassword, $hStoreCity, $hStoreProv, $hStoreAdd, $hStoreAdd2, + $hStorePC, $hStoreCountry, $hStoreEmail, $hStorePhone, $hStoreType, +$hStoreLatitude, $hStoreLongitude, $hStoreFirstName, $hStoreLastName, + $hStoreHPhone, $hStoreCPhone, $hStoreEmail2, hStoreOwner, hStoreAdmi +n, $hStoreAdmin2, $hStoreAdmin3, $hStoreActive, $hStoreWeb, $hStoreLa +ng, $hStoreDesc, $hStoreShowMap, $hStoreComingSoonText, $hStoreComing +SoonTextFrench, $hStoreVanityName, $hStoreWebCreatedDate, $hStoreLast +Login, $hStoreLastEnabled, $hStoreManagerEmpID, $hStoreURL) = split(/ + /, $line); distance($zipcood, $hStoreLatitude, $hStoreLongitude "M") if ("M" < "selectmiles") { $match = 1; $centerinfo = "$hStoreID, $hStoreName\n"; } } } close(FILE, "area.txt")
Thanks in advance...

Replies are listed 'Best First'.
Re: Zip code distance search
by kennethk (Abbot) on Mar 12, 2009 at 16:16 UTC

    In addition to what roboticus says,

    1. Unless you know the difference and intend it, you should use my in place of local.
    2. You should probably use or in place of || - check out operator precedence to see why.
    3. You should probably use the three argument version of open in place of the two, since there are significant security implications.
    4. You probably mean chomp in place of chop.
Re: Zip code distance search
by roboticus (Chancellor) on Mar 12, 2009 at 16:04 UTC
    pglinx:

    The obvious things I see are:

    1. Missing semicolon on distance() call
    2. Not assigning return value of distance() to a variable
    3. Using two constants in if() condition
    4. Using a numeric comparison operator in if() condition rather than string comparison operator (lt)

    HTH,

    ...roboticus
Re: Zip code distance search
by swampyankee (Parson) on Mar 12, 2009 at 18:11 UTC

    I've done ZIP code to great-circle distance calculations, and found a few wormy bits. Of course, the basic idea does have the rather major flaw that just looking at the distance between ZIP codes ignores the rather significant issue of water. Most of Long Island is within about 30 miles of the Connecticut shoreline (where I live), but getting there requires either a ferry ride (about $45, with car) or a route that passes through New York City. I'm sure that similar difficulties occur between the left and right banks of many of the rivers in the US.


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

Re: Zip code distance search
by toolic (Bishop) on Mar 12, 2009 at 16:15 UTC
Re: Zip code distance search
by scorpio17 (Canon) on Mar 12, 2009 at 16:14 UTC
    I think you want something like this:
    my $distance = distance($zipcood, $hStoreLatitude, $hStoreLongitude "M +"); if ($distance < $selectmiles) { $match = 1 ... }
    And use "my" instead of "local"...