CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [Akotb] Sorting data: Edit Log



BillKSmith
Veteran

Sep 10, 2016, 11:13 AM


Views: 11328
Re: [Akotb] Sorting data

This should be close to what you want. It does produce the expected output, at least for this case.

I had to create an auxiliary hash (%aux) to hold all the special data needed for the sort.


Code
use strict; 
use warnings;
use Data::Dumper;

# Data read from file Names.txt in Post #12
# using code pasted from Post #1
my %sportsmans = (
'1000005' => 'Peter Dinklage',
'1000006' => 'Robert Baratheon',
'9887001' => 'Daario Naharis',
'1000001' => 'Jon Snow',
'1000004' => 'Samwell Tarly',
'1234568' => 'Jaime Lannister',
);

# Data read from INPUT.TXT in Post #12
# using code pasted from Post #1
my %rezults = (
'1000001' => [
'107.554', '253.785', '400.589', '560.000',
'709.382', '861.453', '1010.199', '1153.238',
],
'1234568' => [
'81.703', '217.667', '354.691', '491.921',
'631.285', '773.800', '966.925', '1171.632',
],
'1000004' => [
'47.574', '170.425', '292.339', '334.199',
'413.402', '534.960', '657.000', '777.546',
'897.648', '1018.472', '1137.773',
],
'1000005' => [
'71.695', '201.492', '333.976', '463.968',
'593.835', '724.839', '854.015', '983.148',
'1112.136', '1242.781',
],
'1000006' => [
'93.902', '234.171', '375.964', '518.796',
'662.050', '805.207', '947.667', '1092.976',
'1237.113',
],
'9887001' =>
[ '103.242', '242.695', '383.566', '526.097', '671.394', '816.601', ]
);
analyze( \%rezults, \%sportsmans );

sub analyze {
use List::Util qw(min);
my %results = %{ $_[0] };
my %names = %{ $_[1] };
my %aux;
while (my($id, $times) = each %rezults) {
my $name = $names{$id};
my $lap_count = scalar @$times;
my $last_lap_time = $times->[-1] - $times->[-2];
my $best_lap_time = min(@$times); #Ref: post $15
my $last_lap_finish_time = $results{$id}[-1];
$aux{$id} = [
$name,
$lap_count,
$last_lap_time,
$best_lap_time,
$last_lap_finish_time,
];
}
my @order = map {$_->[0]}
sort {$b->[1]<=>$a->[1] or $a->[2]<=>$b->[2] or $a->[3] cmp $b->[3]}
map {[$_, $aux{$_}[1], $aux{$_}[4], $aux{$_}[0]]}
keys %aux
;
my $place = 0;
print "\n\nPlace | Name "
."|Lap time | laps | Best lap time\n\n";
foreach my $pilot (@order) {
printf " %2d | %-20s | %7.3f | %2d | %7.3f\n",
++$place, @{$aux{$pilot}}[0,2,1,3] ;
}
}


OUTPUT:
Place | Name |Lap time | laps | Best lap time

1 | Samwell Tarly | 119.301 | 11 | 47.574
2 | Peter Dinklage | 130.645 | 10 | 71.695
3 | Robert Baratheon | 144.137 | 9 | 93.902
4 | Jon Snow | 143.039 | 8 | 107.554
5 | Jaime Lannister | 204.707 | 8 | 81.703
6 | Daario Naharis | 145.207 | 6 | 103.242

Good Luck,
Bill

(This post was edited by BillKSmith on Sep 10, 2016, 7:55 PM)


Edit Log:
Post edited by BillKSmith (Veteran) on Sep 10, 2016, 7:55 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives