
japhy
Enthusiast
Jan 27, 2000, 7:47 AM
Post #6 of 11
(3234 views)
|
Re: guestbook writes new data to the end of the file
[In reply to]
|
Can't Post
|
|
Sure, I'll explain. You have a file that is to hold messages, like an error log, perhaps. The error log is written to like this: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> open LOG, ">>error_log"; $date = localtime; print LOG "[$date] some-message\n"; close LOG; </pre><HR></BLOCKQUOTE> Now, what you want to do is display these error messages, MOST RECENT FIRST. But you don't want to change the way the error messages are logged, because appending is pretty efficient. So, when you read from the file, instead of push()ing the lines into an array, and then having to reverse the array, or work with the array backwards, you can simple unshift() the lines to the array: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> open LOG, "error_log"; unshift @msgs, $_ while <LOG>; close LOG; for (@msgs) { # do something with $_ } </pre><HR></BLOCKQUOTE> That's what my code does.
|