Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: count lines, bottom to top

by Ieronim (Friar)
on Aug 11, 2006 at 17:59 UTC ( [id://566906]=note: print w/replies, xml ) Need Help??


in reply to count lines, bottom to top

This looks like a typical XY problem. You don't neeed to read the file backwards at all. You can loop through the file throwing away all lines with timestamps greater than you want and then count all lines left.

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;

     s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://566906]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-24 17:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found