in reply to Re^4: Compare fields in a file
in thread Compare fields in a file
Then you add a subroutine normalize which transforms your date-time string into a value (string or numerical) which has the same value for each "slot". For instance:map { ($date, $magnitude) = (split',')[3,4]; [$_, normalize($date), $magnitude] }
As we now get a numeric result and not a string, we also have to replace the cmp in the sort block by a <=> to have the sort work correctly.sub normalize { my $value = shift; my ($hours, $minutes, $seconds) = $value =~ m/(\d{2}):(\d{2}):(\d{ +2}\.\d{3})$/; return int((1000 * $seconds + $minutes * 60 + $hours * 3600)/300); }
With a little effort you can further parametrize the subroutine so you can define the "slot" in a more flexible way without having to rewrite the sub every time.
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
|
|---|