Hi,

I've been trying to solve this challenge for quite awhile, now I'm hoping a Perl expert out there has an elegant solution.

I build an array of hashes from a database table, calculating mileage distance as I insert each record into the structure. Then I sort the array of hashes to come up with the records that have the smallest distance from each client. each tutor has multiple records tied to them thru their ID, but I only want the closest record associated with each. The rest I want to remove from the structure.

In the example below, I only want one of tutor ID 24 and one of ID 50 to show up in the final result.

I'm assuming there may be some way to do this within the sort line I use below, but everything I've tried so far hasn't worked.

Here are my inputs, outputs and code snippets: (thanks in advance for anyone's help!)

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, Zipcod +e 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

Again, thanks!


In reply to sorting an array of hashes and removing duplicates by jmfees

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.