#!/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]};