in reply to Compare fields in a file
When dealing with comma separated data one of the modules Text::CSV and Text::xSV should be your first stop.
When dealing with uniqueness ('only the largest magnitude within each second'second') hashes should spring to mind. Combining those ideas and adding a little error checking (the various sample data you provided were inconsistent) the following should point you in the right direction:
use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new (); my @expectedFields = qw(X Y Z Time Amplitude); # Validate the header line $csv->parse (scalar <DATA>); my @fieldNames = $csv->fields (); die "Unexpected field list: @fieldNames\nExpected: @expectedFields\n" unless @expectedFields == @fieldNames; for my $fieldIndex (0 .. $#fieldNames) { next if $fieldNames[$fieldIndex] eq $expectedFields[$fieldIndex]; die "Got field name $fieldNames[$_]. Expected $expectedFields[$fie +ldIndex]\n"; } # Find maximums in each 1 second slot my %maximums; # Keyed by date/time while (defined (my $line = <DATA>)) { $csv->parse ($line); my ($x, $y, $z, $time, $amplitude) = $csv->fields (); $time =~ s/\.\d{3}//; # Strip fractional seconds next if exists $maximums{$time} && $maximums{$time}{amp} >= $ampli +tude; $maximums{$time}{amp} = $amplitude; $maximums{$time}{line} = $line; } # Output results ordered by time assuming the same date print $maximums{$_}{line} for sort keys %maximums; __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
Prints:
2550,531,66,10-12-2007 07:03:08.069,2 2549,529,62,10-12-2007 07:03:09.151,2
Note: always use strictures (use strict; use warnings;).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Compare fields in a file
by honyok (Sexton) on Feb 10, 2009 at 22:08 UTC | |
by GrandFather (Saint) on Feb 10, 2009 at 23:10 UTC | |
by CountZero (Bishop) on Feb 10, 2009 at 22:24 UTC | |
by honyok (Sexton) on Feb 10, 2009 at 23:07 UTC | |
by honyok (Sexton) on Feb 11, 2009 at 05:10 UTC | |
by CountZero (Bishop) on Feb 11, 2009 at 06:13 UTC |