in reply to Compare fields in a file
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.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
<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 | |
by CountZero (Bishop) on Feb 10, 2009 at 22:10 UTC |