in reply to Compare fields in a file

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

Replies are listed 'Best First'.
Re^2: Compare fields in a file
by hbm (Hermit) on Feb 10, 2009 at 21:08 UTC

    Nice! If I'm not mistaken, you can shorten it still:

    use strict; use warnings; my $previous = ''; print grep { $_ } map { if ($previous ne $_->[1]) { $previous = $_->[1]; $_->[0]; } } sort { $a->[1] cmp $b->[1] or $b->[2] <=> $a->[2] } map { my ($date, $magnitude) = (split/,/)[3,4]; [ $_, join("", (split(/[-:. ]/,$date))[2,1,0,3,4,5]), $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

    Gives me the same output as yours.

    Update:Shortened a bit more and added strictures.

    Update2: CountZero, thanks for pointing out the problem - it was subtle. I added a simple grep to fix it, which pretty much brings it back to your solution. Ah well, good stuff.

      Actually, there is a subtle difference: yours outputs an empty string for the elements which should be discarded. When printing this doesn't matter, but when saving it into an array, you will have lots of empty elements sprinkled through the array. Hence my use of grep which doesn't output anything if the value of its block is false.

      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