http://www.perlmonks.org?node_id=946315


in reply to Need help get longest timediff

Like ambrus's solution, Time::Piece doesn't recognize fractions of a second, (I believe), and I had to edit them out before feeding to the the strptime method for Time::Piece.

I noticed that the beginning times for each like Team-City pairing was the same. I didn't know if this was coincidence or that they would be the same in every case. This solution doesn't assume that.

#!/usr/bin/perl use strict; use warnings; use Time::Piece; # core since Perl v5.9.5 my @data; open my $fh, "<", 'o33.txt' or die $!; while (<$fh>) { push @data, [ split /,/ ]; my ($start, $end) = map Time::Piece->strptime($_, "%Y-%m-%d %H:%M: +%S"), map s/\.\d\d\d$//r, @{ $data[-1] }[2,3]; push @{ $data[-1] }, $end - $start; # piggyback elapsed seconds $d +ata[r][c] } close $fh or die $!; my %seen; my @lines = grep {!$seen{ "@$_[1,4]" }++} sort {$a->[1] cmp $b->[1] || $b->[-1] <=> $a->[-1]} @data; for my $rec (@lines) { printf "%s,%s,%0.2f hours\n", @$rec[1,4], $rec->[-1]/3600; } __END__ *** input (o33.txt) 1,BEAR,2011-11-21 08:49:16.000,2011-11-21 08:53:13.910,San Francisco,, +, 2,BEAR,2011-11-21 08:49:16.000,2011-11-21 12:50:31.550,San Francisco,, +, 3,BEAR,2011-11-21 08:49:16.000,2011-11-21 08:49:19.987,San Francisco,, +, 4,HAWK,2011-11-21 10:36:26.000,2011-11-21 10:45:11.823,Los Angeles,,, 5,HAWK,2011-11-21 10:36:26.000,2011-11-21 12:41:17.763,Los Angeles,,, 6,HAWK,2011-11-21 10:41:12.000,2011-11-21 10:55:08.393,San Francisco,, +, 7,HAWK,2011-11-21 10:41:12.000,2011-11-21 15:46:24.707,San Francisco,, +, 8,HAWK,2011-11-21 10:41:12.000,2011-11-22 11:09:13.907,San Francisco,, +, *** output BEAR,San Francisco,4.02 hours HAWK,San Francisco,24.47 hours HAWK,Los Angeles,2.08 hours