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

I am trying to sort this by distance (km)... All works but I just can't seem to get this to happen
open(FILE, "herbalarea.pl") || die "I can't open that because: $!\n"; while(<FILE>) { chop; @all = split(/\n/); foreach $line (@all) { local($hStoreID, $hStoreLatitude, $hStoreLongitude) = split(/ /, + $line); $lat2 = $hStoreLatitude; $lon2 = $hStoreLongitude; $lat1 = $lat1a; $lon1 = $lon1a; # Notice the 90 - latitude: phi zero is at the North Pole. @L = (deg2rad($lat1), deg2rad(90 - $lon1)); @T = (deg2rad($lat2), deg2rad(90 - $lon2)); $km = great_circle_distance(@L, @T, 6378); #-------HERE IS THE PROBLEM---------# @km=sort {$a <=> $b} (@zip_dist); $km = (@zip_dist); #-----------------------------------# if ($km < $distance) { $match = 1; $count ++; $area = "$hStoreName <BR> $hStoreAdd <br> $hStoreCity, $hStoreProv + $hStorePC\n"; $rounded = sprintf("%.1f", $km); print " $area $km \n"; }

Replies are listed 'Best First'.
Re: Sort by distance
by ikegami (Patriarch) on May 27, 2009 at 13:52 UTC

    Use use strict; use warnings;!!!

    @zip_dist is never assigned anything.

    $km = (@zip_dist); surely doesn't do what you want it to do. An array in scalar context returned the number of elements in the array.

    $count++; is rather useless outside of a loop.

Re: Sort by distance
by jettero (Monsignor) on May 27, 2009 at 13:53 UTC

    (copy ikegami) and: I imagine you want my not local. You rarely, if ever, want local. You also seem to be missing a closing brace.

    I was trying to reformulate this as a Schwartzian transform, but I'm confused about what it is that you're sorting. I'd expect something like this to work, but it wont if you're comparing the distance between $a and $b -- what would the usefulness of that be?

    my @sorted_points = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map {[$_, dist($_, $reference_point)]} @points; # points is an AoA structure

    -Paul

Re: Sort by distance
by Anonymous Monk on May 27, 2009 at 13:57 UTC
Re: Sort by distance
by DStaal (Chaplain) on May 27, 2009 at 13:56 UTC

    Two questions: Where, and how, do you define @zip_dist? It's hard to figure out what the problem with a sort is if we can't see the data that's being sorted.

    And number 2: What are you trying to do in line #22? Whatever it is, something should probably have a different name. (You've got @km and $km, and I think you are trying to use them as separate variables. Which can be done, but it's going to confuse you, your maintainer, or perl. Or all three.)

      The section shown as the problem was nothing more then an attempt at sorting but causes the entire database to output with km equaling 0 I probably should have asked the question leaving that part out. I eliminated thing that have to do with count and other funtions to make it more brief and example.

        Well, you eliminated too much: The example above doesn't have enough information to answer your question.

Re: Sort by distance
by hda (Chaplain) on May 27, 2009 at 16:11 UTC
    Hi, it might be a little of an overkill for your needs, but consider using PostGIS for your most complicated geographical vector analyses. Having your data points in a PostgreSQL database and accessing it with DBI is a great idea. I use it almost on a daily basis.