in reply to count lines, bottom to top
The next problem is that you want to read data from file and then append a new timestamp at the end. For this, you need to open the file for both reading and writing, and AFAIK call seek to swith from reading to writing. In general, calling seek will not cause more problems if it is not necessary.
my $times = 2; my $minutes_threshold = 7200; my $now = time(); my $oldest_time = $now - ($minutes_threshold*60); my $user_status_file = "C:/temp/perl_logs/user_status.$user.txt"; open (USF, "+<", $user_status_file) or die "Failed to open User Status + File file: $!"; # Now, read through the lines of the file # and see if n times occured in the last m minutes. my $count = 0; while ( <USF> ) { chomp $_; next if ( $_ < $oldest_time ); $count++; } if ( $count > $times ) { print ADL "User exceeded threshold! $count \n"; } seek USF, 0, 2; #go to the end of the file - maybe you can remove + this line print USF "$now\n"; close USF;
|
|---|