Database records: ID, tutorID, Latitude, Longitude, City, State, Zipcode 1, 24, 39.948927, -75.1586630, Philadelphia, Pennsylvania, 19107 2, 24, 44.813947, -93.9194250, Young America, Minnesota, 55555 3, 24, 34.270548, -119.217135, Ventura, California, 93003 5, 6, 35.938033, -78.1193720, Spring Hope, North Carolina, 27882 6, 50, 32.357439, -111.044532, Tucson, Arizona, 85741 7, 5, 41.299209, -96.0521530, Omaha, Nebraska, 68134 8, 50, 38.921460, -119.117640, Yerington, Nevada, 89447 9, 7, 40.798205, -73.7382850, Great Neck, New York, 11027 ################## Perl code snippet ###################### $i=0; $sql = qq|select ID, tutorID, Latitude, Longitude, City, State, Zipcode from $table_tzips where Zipcode IS not NULL|; $sth = $dbh->prepare($sql); $sth->execute(); $tzips = $sth->fetchrow_hashref(); while (defined $tzips) { $trdist = &calculate_distance($clong, $clat, $tzips->{Longitude}, $tzips->{Latitude}); ($tutorsdata[$i]{trID}, $tutorsdata[$i]{tutorsID}, $tutorsdata[$i]{tmiles}, $tutorsdata[$i]{city}, $tutorsdata[$i]{state}, $tutorsdata[$i]{zipcode}) = ($tzips->{ID}, $tzips->{tutorID}, $trdist, $tzips->{City}, $tzips->{State}, $tzips->{Zipcode}); $i++; $tzips=$sth->fetchrow_hashref(); } $sth->finish; @sortthetutors = sort{ $$a{tmiles} <=> $$b{tmiles} } @tutorsdata; print "Tutor Distances:\n\n"; for $printtutors(@sortthetutors) { print "$$printtutors{trID}, $$printtutors{tutorsID}, $$printtutors{tmiles}, $$printtutors{city}, $$printtutors{state}, $$printtutors{zipcode} \n"; } ################## END Perl code snippet ###################### Current output of this: 3, 24, 0, Ventura, California, 93003 8, 50, 321, Yerington, Nevada, 89447 6, 50, 489, Tucson, Arizona, 85741 7, 5, 1348, Omaha, Nebraska, 68134 2, 24, 1522, Young America, Minnesota, 55555 5, 6, 2307, Spring Hope, North Carolina, 27882 1, 24, 2432, Philadelphia, Pennsylvania, 19107 9, 7, 2502, Great Neck, New York, 11027 Desired output: 3, 24, 0, Ventura, California, 93003 8, 50, 321, Yerington, Nevada, 89447 7, 5, 1348, Omaha, Nebraska, 68134 5, 6, 2307, Spring Hope, North Carolina, 27882 9, 7, 2502, Great Neck, New York, 11027