CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
So close .. so very, very close

 



Anthony
Deleted

Mar 27, 2000, 4:38 PM

Post #1 of 2 (924 views)
So close .. so very, very close Can't Post

Been around more than once on similar topics and I am so close to a result I can taste it (ASCII, for those of you that don't know, is sorta tangy, sorta sweet).

So, scenario:

Each time a user hits the submit button, the following is supposed to happen.

1)User name and current time is written to a file in the form: $user|$time
2)Check to see if no one has posted in ten minutes, and if they haven't, I assume they are no longer active and they should be erased from the list.

So, I have come up with this:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$date = time();
$late = $date - 600;
$old = "$whodir/$whofile";
$new = "$whodir/$whofile.tmp";

open (NEW,">$new") or die "Egads!: $!";
flock(NEW, 2);

%userlist = ();

foreach $line (@append) {
@edit = split(/\|/, $line);
$userlist{$edit[0]} = $edit[1];
}
while ( ($k,$v) = each %userlist) {
if ($v <= $late) {
delete $userlist{$k};
}
if ($k eq $user) {
$userlist{$k} = $date;

}
else {
$userlist{$k} = $v;

}
}
while ( ($k,$v) = each %userlist) {
print NEW ("$k|$v");
}
flock(NEW, 8);
close (NEW);

rename ($new, $old);
</pre><HR></BLOCKQUOTE>

First, this isn't the whole code .. the rest creates OLD, reads OLD and etc. This section is supposed split the entries, push them into a hash as key,value .. reassemble the hash (deleting keys that have $late values) and print out the new hash (with keys that have no late values).

Problem: Am I evaluating time wrong? Cause when the NEW is recreated, the late user is still there.

Anthony

[This message has been edited by Anthony (edited 03-27-2000).]


summer
Deleted

Mar 31, 2000, 12:41 PM

Post #2 of 2 (924 views)
Re: So close .. so very, very close [In reply to] Can't Post

Anthony,

You've cut out some code so it is difficult to be certain of what is going wrong. However there are clearly some issues with your code that you might consider addressing.

The impression I have is that you are reading in an entire file, then chopping out the unwanted entries then writing out the new file.

If this assumption is correct (and assuming that the file never gets too large) then the approach I would take is:
1. Open the input file;
2. Attempt to lock it - quit or retry a few times then quit if you can't get the lock - (code as written doesn't check to see if you get the lock or not.);
3. Read the data;
4. Close input file;
5. Read through the data in memory deleting the elements you don't want;
6. Open the output file;
7. Attempt to lock it or quit etc as 2 above;
8. Write the data.

Note that close() will unlock for you.

I would recommend storing datetime|user just in case user contains a | which would confuse things (datetime never will if you use the time function).

I can't see why you need a hash; it takes more memory and is slower than an array and doesn't seem to give you anything more.

Could try something like the (untested) code below:

# Assume @input is populated
my @output ;
foreach my $record ( @input ) {
my @record = split /\|/, $record ;
push @output, $record
if $record[0] > $late ; # Only keep recent entries
}
# Now we write the output.

Of course the above takes two arrays i.e. twice the space but probably not twice as much as a single hash for the same data. But if you're wedded to a hash then you could try:

while( my( $user, $date ) = each %userlist ) {
delete $userlist{$user}
if $date <= $late ;
# No need to reinsert, each is non-destructive
}
while( my( $user, $date ) = each %userlist ) {
# I've assumed your original order
print NEW "$user|$date\n" ;
# Note you almost certainly will need the \n to put each record on a separate line.
}

If the data could become quite large then you might want to consider using a tied hash, but that's a different story.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives