I would think that doing as much of the work as possible in the SQL query, as suggested by other Monks, makes most sense. I have not done enough SQL to usefully contribute there. Looking at the documentation for
DBI I saw the
fetchall_arrayref method. If going the simple query route, you could use it to populate a hash keyed by service_id with the results of the query sorted by descending distance. Thus the shortest distance row for each service would be assigned to the hash last, overwriting any longer distances. I don't have DBI available (or a database for that matter) so have made up a pretend routine that returns a ref. to an AoA and I added a little more pretend data.
use strict;
use warnings;
use Data::Dumper;
sub pretendFetch
{
my $cannedFetch = [
[6200249230, 2, 247.695377429616],
[6200200581, 1, 131],
[6200249220, 2, 394.81641303269],
[6200249240, 2, 148.32734070292],
[6200249269, 1, 127.84583439484],
[6200249250, 2, 394.507287638644],
[6200248437, 1, 234.38349845484],
];
return $cannedFetch;
}
my %service_id =
map { $_->[1] => {
stop_reference => $_->[0],
distance => $_->[2]
}
}
sort { $b->[2] <=> $a->[2] }
@{ pretendFetch() };
print Data::Dumper->Dump([\%service_id], [q{*service_id}]);
Which produces
%service_id = (
'1' => {
'distance' => '127.84583439484',
'stop_reference' => '6200249269'
},
'2' => {
'distance' => '148.32734070292',
'stop_reference' => '6200249240'
}
);
I hope this is of interest.
Cheers,
JohnGG
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.