in reply to Re^2: Compare fields in a file
in thread Compare fields in a file

Does this do what you want? It stuffs all the lines into a hash-of-hashes data structure. The primary key is the time, truncated to seconds. The secondary key is the magnitude. First, it sorts by time, then by magnitude, keeping only the largest magnitude.

Update: This code needs to be adapted if the input file contains data for more than one day.

use strict; use warnings; my %mags; while (<DATA>) { next if /X/; chomp; my $pair = (split)[-1]; my ($time, $mag) = split /,/, $pair; $time =~ s/\..*//; $mags{$time}{$mag} = $_; } for my $time (sort keys %mags) { my $mag = (sort {$b <=> $a} keys %{ $mags{$time} })[0]; print "$mags{$time}{$mag}\n"; } __DATA__ X,Y,Z,Time,Amplitude 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,2

This prints:

2550,531,66,10-12-2007 07:03:08.069,2 2549,529,62,10-12-2007 07:03:09.151,2

Replies are listed 'Best First'.
Re^4: Compare fields in a file
by CountZero (Bishop) on Feb 10, 2009 at 20:29 UTC
    Nice, but it does not take into account the date portion of the date-time. If you have several days of data it will lump together all the same times, not looking at the day.

    I grant you that it is not very clear from the OP's example whether his file can contain multiple days.

    I looked again at the program written by the OP and indeed he only uses the time element, so you were right!

    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

      I couldn't decide whether it was relevant to mention this "feature" of my code. So, in my Laziness, I decided to omit that assumption. I think I'll update my node to be more explicit.