in reply to Re^2: Sort by distance
in thread Sort by distance

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

Replies are listed 'Best First'.
Re^4: Sort by distance
by pglinx (Acolyte) on May 27, 2009 at 14:53 UTC
    Here is the entire code that works but I need to sort by km
    print " </td> <TD VALIGN=TOP WIDTH=350 style=\"font-family: arial; font-size: 12px;\ +"> <IMG SRC=\"\" BORDER=0 ALT=\"\"> <BR>Listings Found In Radius ( $count ) <div class=\"scrollbox\"> <font face=arial size=2>\n"; $count = 0; open(FILE, "herbalarea.pl") || die "I can't open that because: $!\ +n"; while(<FILE>) { chop; @all = split(/\n/); foreach $line (@all) { local() = 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); if ($km < $distance) { $match = 1; $count ++; $area = ""; $area1 = ""; $area2 = ""; $rounded = sprintf("%.1f", $km); print " <table border=0 width=240 height=105 cellpadding=15 cellspacing=0 back +ground=\"\"> <tr> <td style=\"font-size: 11px; color: #5D5D5D;\"> Store # <A HREF=\"javascript:change1('pic1','image$count')\">$area</A> +<BR>\n"; print "<div class=\"book\"><A HREF=\"\" target=_parent>Book Your Free +Consultation.</a> - KM $rounded</div></A></td> </tr> </table> \n";

      Ok, I don't see any sort in there. What's your plan for sorting things? For that matter, where are the two arrays you were using in your initial post? I don't see either of them here.

      You are probably going to need some data structure that relates things together (a hash, of some sort), which would then be in an array for each line, which you can then sort and use.

      I see that you have a huge assignment with a split. Couple of points that may help you reduce "the noise level:...while (<FILE>) is iterating over the lines in the file. This split on \n appears to do nothing. Default split will split on whitespace and use $_. \n is part of whitespace, so chomp() is isn't needed. Use "array slice" to get the variables that you really need. Don't create needless vars that you don't use.Something like this:
      while (<FILE>) { my($hStoreAdd $hStoreAdd2 $hStoreCity)=(split)[7,8,10]; #....add more vals above as you need, you can also use a range # of indicies as long as the range is lowest to highest, # [9,10,2..4] is ok but 4..2 would not be ok. }
      I also still don't see the sort problem yet.

      UPDATE:

      I looked back again this question.
      It appears to me that what you have here is a loop that loops on lines from some DB and you are able to print them to some kind of HTML format.

      The value distance in km is a derived value from the input data. So to get the printout to come out in a different order than the order provided by the DB in the loop, you need to save the data for each print, then sort that data structure, then print it.

      You have a lot of values, less than probably you create in the split, but still a lot. So, one choice for the data structure is a LoH (list of hash), some folks would say AoH (Array of Hash). This allows you to have nice names that can be used later when doing the print.

      Try something like this:

      #!/usr/bin/perl -w use strict; my @km_testlist = qw( 120 500 300 400); my @LoH; #this will be a list of references to hash tables #now of course this just shows how to make and #sort this type of structure. #the stuff in this loop goes into your line process loop.. foreach my $thiskm (@km_testlist) #building a test structure { my %table = (); #don't need the =() but makes purpose very clea +r. $table{'val1'}="something"; #this data doesn't matter $table{'val2'}="aaaa"; $table{'km'}=$thiskm; push(@LoH,\%table); #pushes a ref to table hash onto @LoH } @LoH = sort { my $a_km = $a->{'km'}; #don't need the $a or $b_km temp my $b_km = $b->{'km'}; #trying to be clear for you $a_km <=> $b_km }@LoH; #so now instead of using the $vars, from each line, you use #the values in the hash, instead of 'val1', I would give the #sort of name you are already using. foreach my $tab_hashref (@LoH) { print "$tab_hashref->{'km'} $tab_hashref->{'val1'} $tab_hashref->{' +val2'}\n"; } #prints: #120 something aaaa #300 something aaaa #400 something aaaa #500 something aaaa