Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update procedure for message board 1

Status
Not open for further replies.

vego

Programmer
Jun 26, 2004
47
US
Hi, everyone. I've written a simple message board and all works well, but I would like to have any posts that are replied to, go to the top line in my flat file. As it stands, the method below updates the $replies just fine and all, but I can't figure out how to make the update "AND" move the "UPDATED" line to the "TOP".

open(DAT, "+<$dir/$db") || die error( $! );
flock(DAT, LOCK_EX) || die error( $! );
my @dat = <DAT>;
seek(DAT, 0, 0) || die error( $! );

foreach (@dat) {
($thread,$subject,$name,$replies,$date) = split(/\|/);
if (param('thread') == $thread) {
$replies++;
print DAT "$thread|$subject|$name|$replies|$date";
} else { print DAT $_; }
}

close(DAT) || die error( $! );


Below is how it appears "before" updating FIRST POST with the above code:

3|THIRD POST|Robert|0|Sat Jun 26 13:54:25 2004
2|SECOND POST|Jerry|0|Sat Jun 26 13:54:02 2004
1|FIRST POST|Susan|0|Sat Jun 26 13:53:35 2004

And then after updating, it looks like below:

3|THIRD POST|Robert|0|Sat Jun 26 13:54:25 2004
2|SECOND POST|Jerry|0|Sat Jun 26 13:54:02 2004
1|FIRST POST|Susan|1|Sat Jun 26 13:53:35 2004


This is how I would like it to read: (the newly updated line at the top)

1|FIRST POST|Susan|1|Sat Jun 26 13:53:35 2004
3|THIRD POST|Robert|0|Sat Jun 26 13:54:25 2004
2|SECOND POST|Jerry|0|Sat Jun 26 13:54:02 2004

Is it possible to have this happen? If so, can anyone see a solution?
 
Look up tie::file and reverse
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Thank you, Paul, for your response. Unfortunately, I can't seem to find what you are pointing to in Tie::File. I did a search for "reverse" and the word does not appear anywhere in the module. Can you expound further on exactly what I should be exploring?
 
To my understanding if you Tie a file it becomes an array which you can reverse and spit it back out then

--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Ok, I've explored Tie::File and it seems that I can do just about everything as one could using pop, push, shift, unshift and splice, but I don't see a reason to use reverse...as I'm not really needing to reverse anything. I just need the updated line to go to the top and everything else to stay where it is. Any tips would be appreciated.
 
Code:
open FILE2, "$$.txt" # use the pid as a temp file name
print FILE2 "3|Blah Blah Blah";
close FILE2;
#on NT, W2k, or XP
system ("copy $$.txt+original_file.txt temp_file.txt");
system ("rename temp_file.txt original_file.txt");
system ("delete $$.txt");
system ("delete temp_file.txt"); # if it still exists?

Don't know what the *nix variant of this is
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Well, Paul, since I locked up the hard-drive on my XP 'chine, I've been using my old Win98, so, obviously I wasn't able to use your code, but you did open my eyes to the temp file routine and I've managed to conjour up a working version that does exactly as I had hoped for at the outset. Of course my version is inefficient, (I need to open and close more files than I really expected), but, nonetheless, I got it to go. Thank you for your help.
 
You're Welcome
--Paul

There's more than one way, that cat's only got eight left ...

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top