use strict;
use warnings;
use Tie::Handle::CSV;
my $log_file = Tie::Handle::CSV->new('userlog', header => 1);
my %last_contact;
while ( my $log_entry = <$log_file> )
{
my $userA = $log_entry->{'userA'};
my $userB = $log_entry->{'userB'};
my $start = $log_entry->{'start-time'};
my ($h,$m,$s) = split /:/, $start;
$start = $h * 3600 + $m * 60 + $s;
my $end = $log_entry->{'start-time'};
my ($h,$m,$s) = split /:/, $end;
$end = $h * 3600 + $m * 60 + $s
if ( ( $last_contact{ $last_contact->{user}->{$userA} } eq $userA )
&& defined $last_contact{ $userA }->{'finish'}
&& ( $last_contact{ $userA }->{'finish'} - $start < 75 ))
{
print "Users $userA and $userB have consecutive sessions in less than 75 seconds\n";
}
else
{
$last_contact{$userA}->{'user'} = $userB;
$last_contact{$userA}->{'finish'} = $end;
$last_contact{$userB}->{'user'} = $userA;
$last_contact{$userB}->{'finish'} = $end;
}
}
所以,我会使用一个哈希值,它的键是用户,值是他们的最后一次联系。如果用户的最后一个联系人被用作键并指向第一个用户,那么他们就是他们的最后一个联系人,你需要采取一些行动(这里我只是打印出这个事实)。否则,将这两个用户设置为彼此的最后联系人。
编辑以跟踪时间并确保它低于 75 秒。