Only one line (not counting the __DATA__ section)!
print map { $_->[0] } grep { if ($previous eq $_->[1]) { $previous = $_->[1]; 0; } else { $previous = $_->[1]; 1; } } sort { $a->[1] cmp $b->[1] or $b->[2] <=> $a->[2] } map { (undef, undef, undef, $date, $magnitude) = split ','; $date =~ m/(\d{2})-(\d{2})-(\d{4}) (\d{2}:\d{2}:\d{2})/; $date_sort = "$3$2$1$4"; [$_, $date_sort, $magnitude] } <DATA>; __DATA__ 2550,531,66,10-12-2007 07:03:08.069,2 2549,529,62,10-12-2007 07:03:08.151,1 2550,531,66,10-12-2007 07:03:09.069,1 2549,529,62,10-12-2007 07:03:09.151,10 2549,529,62,10-12-2007 07:03:09.151,2 2549,529,62,10-12-2007 07:03:09.151,7 2549,529,62,10-12-2007 07:03:09.151,2 2549,529,62,10-12-2007 07:03:09.151,8 2549,529,62,10-12-2007 07:03:09.151,2 2549,529,62,10-02-2007 07:03:10.151,2 2549,529,62,10-12-2007 07:13:09.151,2 2549,529,62,10-12-2007 07:03:09.151,2 2549,529,62,10-12-2007 17:03:09.151,1
It uses a Schwartzian Transform for speed and efficiency. To follow the flow of this script, you have to start at the end and work to the beginning.

<DATA> in a list context returns all the data as one list.

The map at the end sets up a data-structure: an Array of Arrays. Each of its elements contains the original value, the reworked date-time value so we can sort on it directly and finally the magnitude.

sort then sorts on the reworked date-time element and (in case of equality) next on the magnitude (largest first).

grep checks the reworked date-time value to see if we have not yet seen it before and if it is the first one (meaning its magnitude will be the largest) lets it through.

The map closest to the print transforms the data-structure back into the original value (which was saved in the element with index 0).

Finally print displays everything. As each data-element ends with EOL, we get a nice print-out of one element per line.

Update: If only the time element of the date-time is to be used and the date element is to be disregarded, replace $date_sort = "$3$2$1$4"; by $date_sort = $4;

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


In reply to Re: Compare fields in a file by CountZero
in thread Compare fields in a file by honyok

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.