PDA

View Full Version : Appending to file in Perl...


Tham
02-16-2006, 01:07 PM
So I have a file that prints to another file, but its a newbulliten so idealy, I would want the latest post to be on top. So the obvious answer would be to append to the file but to append to the top. I've tried a lot of things, but none of them are working, can someone help me? I tried to read the file into an array, print my sections to it, and then print the rest of the file, I've tried imputting data, its just not working. Can someone help me?

Tham
02-17-2006, 10:44 AM
no one knows how? I know that you can append to a file with
open (FILE, >>myfile.html)

but how could I append to the top?
Is this too newbie of a question?

AnthonyP
02-18-2006, 09:23 PM
i don't remeber perl much ... but one way you could do it is read the initial file and hold that data... then overwrite the file with the new data and append the data your holding into that overwritten file...

For example (news.txt)
1) Read news.txt and hold it or save to a temp file

2) Overwite news.txt with the new post

3) Take the data ur holding or that u saved to temp file and write it appeneded to forum.txt

This way ur new post is on top and everything else is written after it...

Tham
02-18-2006, 11:58 PM
That was my first Idea, but I couldn't get the syntax down. Any Ideas?

AnthonyP
02-19-2006, 12:34 AM
Ok.. ill try to write up some code, but im really rusty on my Perl so if this dosen't work... play with it a bit until you can get it to work! Also... try it out on a test file first, don't play with any important data...



$my_file="news.txt";
open(TXT, $my_file) || die("Could not open the file!");
@raw_data=<TXT>;
close(TXT);



This will put all the contents of the file into the array @raw_data... The next step is to overwrite your new post into the file news.txt ... Since this will basically erase everything else, now you can take the stuff from the array and append it to the file!



open(TXT,">$my_file") || die("Cannot Open the File");
print TXT "My Latest News: Blah Blah Blah \n \n";
close(TXT);


open(TXT,">>$my_file") || die("Cannot Open the File");
foreach (@raw_data) {
print $_;
}
close(TXT);



Try it out... hope it works lol! if not... good luck :crying:

Tham
02-25-2006, 12:08 PM
it kinda almost worked. (Not really :-P) I figured it out though. Check it out here:
http://www.monthlyspecials.com/Message%20Board/AddMessage.html
and see your posts
http://www.monthlyspecials.com/Message%20Board/MessageBoard.shtml
Thanks for the help,
Tham.

AnthonyP
02-26-2006, 02:14 AM
YAY! :) glad it worked! ...what was worng by the way? :P just curious!

Tham
03-03-2006, 02:34 PM
just syntax really. Reading my file into the array and printing it back out.

TrojanWarBlade
03-03-2006, 05:31 PM
Why not simply rename files and then append file 2 to the end of file 1?
It's all a matter of how you think about things.

Tham
03-04-2006, 08:13 AM
because you still have to read file 2 into an array and do the same exact thing. and then you would have to erase and recreate the second file everytime to start with new data...

TrojanWarBlade
03-05-2006, 05:09 PM
So you don't think this would be any good then?

#!/usr/bin/perl -w
use strict;
my $newsfilename = "newsfile.txt";
my $tempfilename = "tmp.txt";
my $newrecord = shift;
open OUT, ">$tempfilename" or die "failed to open temp file";
print OUT $newrecord, "\n";
if(open IN, "$newsfilename") {
while(<IN>) {
print OUT $_;
}
close IN;
}
close OUT;
rename $tempfilename, $newsfilename;

Tham
03-06-2006, 11:13 AM
well then you have to create a new temp file each time. But then what happens when something bugs in your temp file? the whols system is gone. Yours will work. Just theres a potential for things to get messed up. I think it should work if you recreate the tempfile each time though.

TrojanWarBlade
03-07-2006, 07:40 PM
Full of positive comments again I see.
The code works. If the tempfile is an issue then there are ways of dealing with that.
It is generally very unwise to try to read log files into memory, especially into arrays cos they tend to get big and things tend to fail.
This code will work forever so long as it has a safe place to work and it doesn't run out of disk space (but then again, if that happens you have a whole other problem!).