If you are indeed using a hash to hold times, this should do more or less what you want (this is what I use for something similar. The time distance is 3 minutes, so 3 minutes after they leave their name is removed.):
use Data::Dumper;
#Next 2 lines make sure only one HTTP request deals with database at o
+nce
open LOCKFILE, ">>lock.lck" or die "Could not open the lock file!";
flock LOCKFILE, LOCK_EX or die "Could not flock the lock file!";
#database.dat holds the physical hash
#a 'do' executes the file (rebuilds your time hash)
do "database.dat";
#Get the current date values
@date = localtime();
#loop through the %userTimes hash to find old entries
foreach $user (keys(%userTimes)) {
$last_hour = $userTimes{$user}{'hour'};
$last_min = $userTimes{$user}{'min'};
$cur_hour = $date[2];
$cur_min = $date[1];
$expire = 0;
#Yes, if 3 minutes old
$expire = 1 if ($last_hour == $cur_hour && ($cur_min - $last_min) >= 3
+);
#there are some exceptions to the 3 minute deal.
#the next lines cover this!
if ($last_hour != $cur_hour) {
$expire = 1 if $last_hour > $cur_hour;
$expire = 1 if $last_min <= 57 && $cur_min >= 0;
$expire = 1 if $last_min == 58 && $cur_min >= 1;
$expire = 1 if $last_min == 59 && $cur_min >= 2;
}
if ($expire == 1) {
delete $userTimes{$user}; #yep, delete them!
}
}
#Output the hash to the database file thingy
$Data::Dumper::Purity = 1; #Need this
$Data::Dumper::Indent = 0; #saves some disk space
open FILE, ">database.dat";
print FILE Data::Dumper->Dump([\%userTimes], ['*userTimes']);
close FILE;
That's about it! The only thing you really need to concern yourself with is that subtracting the minutes is not good enough. There are some 'race conditions' you need to look at. :)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|