G'day rruser,

Here's a solution using the builtin modules Time::Piece and Time::Seconds. It only requires a single script (instead of your three) and doesn't create an additional file concatenating all the data from the original three files.

With the data you posted, "COMPANY      BCYX 156095 L  2013,11,30   2013,12,08" has the greatest time spread (8 days). For my testing, I duplicated that record in file2 and file3 and added a unique record to file1 which also had a spread of 8 days. Here's the data I worked with:

$ cat pm_1071030_file1.txt COMPANY ABCD 764200 E 2013,12,13 2013,12,19 COMPANY BCDX 156167 L 2013,11,29 2013,12,03 COMPANY BCYX 165230 L 2013,12,13 2013,12,19 COMPANY BCYX 156095 L 2013,11,29 2013,12,07 $ cat pm_1071030_file2.txt COMPANY ABCD 764200 E 2013,12,13 2013,12,19 COMPANY BCDX 156167 L 2013,12,28 2013,12,31 COMPANY BCYX 156095 L 2013,11,30 2013,12,08 $ cat pm_1071030_file3.txt COMPANY ABCD 764200 E 2013,12,13 2013,12,17 COMPANY BCDX 156167 L 2013,11,30 2013,12,03 COMPANY BCYX 165230 L 2013,12,13 2013,12,17 COMPANY BCYX 156095 L 2013,11,30 2013,12,08 COMPANY BCYX 156095 L 2013,11,30 2013,12,08

Here's the script:

#!/usr/bin/env perl use strict; use warnings; use autodie; use Time::Piece; use Time::Seconds; my $format = '%Y,%m,%d'; my $free_days = 4; my @wanted_record_data = (0, []); for my $file (map { "pm_1071030_file${_}.txt" } 1 .. 3) { open my $fh, '<', $file; while (<$fh>) { my ($start, $end) = (split)[4, 5]; my $t0 = Time::Piece->strptime($start, $format); my $t1 = Time::Piece->strptime($end, $format); my $diff = ($t1 - $t0)->days; next if $diff <= $free_days; push @{$wanted_record_data[1]}, $_ if $diff == $wanted_record_ +data[0]; @wanted_record_data = ($diff, [$_]) if $diff > $wanted_record_ +data[0]; } } print "Greatest time spread = $wanted_record_data[0] days.\n"; print "Records with this time spread:\n"; my %seen; print for grep { ! $seen{$_}++ } @{$wanted_record_data[1]};

Here's the output:

Greatest time spread = 8 days. Records with this time spread: COMPANY BCYX 156095 L 2013,11,29 2013,12,07 COMPANY BCYX 156095 L 2013,11,30 2013,12,08

-- Ken


In reply to Re: Working with multiple records with different dates by kcott
in thread Working with multiple records with different dates by rruser

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.