Here's a start. This is actually pretty close to the algorithm that nefigah described (though I wrote it before reading that).

use strict; use warnings; use Data::Dumper; my $last_hour = 23; my %best_of; while (<DATA>) { my ( $quote, $time ) = m{ \A # beginning of line ( [^,]+ ) # non-commas \s* , \s* # comma with optional spaces ( # open capture \d\d? # hours : \d\d # minutes : \d\d # seconds ) }xms; my ( $hour, $min, $sec ) = split /:/, $time; if ( '00' eq $sec && '00' eq $min && -1 == --$hour ) { $hour = $last_hour; } my $seconds_past = $min * 60 + $sec; if ( ! $seconds_past || $best_of{ $hour }{second} < $seconds_past ) { $best_of{ $hour } = { second => $seconds_past, time => $time, quote => $quote, }; } } print Dumper \%best_of; __DATA__ 1.53311 ,1:59:52 1.53311 ,1:59:5220 1.53311 ,1:59:52 1.53311 ,1:59:52hi 1.53311 ,2:00:00 1.53306 ,2:00:03 1.53307 ,2:00:06

Here's the output:

$VAR1 = { '1' => { 'quote' => '1.53311 ', 'time' => '2:00:00', 'second' => 0 }, '2' => { 'quote' => '1.53307 ', 'time' => '2:00:06', 'second' => 6 } };

This pops out a couple of warnings ("Use of uninitialized value in numeric lt (<)") in the last condition because it's comparing $seconds_past to an undef that gets autovivified in %best_of.

Anyway, what you end up with is a hash with each hour seen as a key. The values are hash refs that contain the data you're interested in.


In reply to Re: reading a delimited file and selecting values from it by kyle
in thread reading a delimited file and selecting values from it by Conal

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.