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:
Adding to the beginning of a file

 



muinatit
Deleted

Feb 16, 2001, 8:52 AM

Post #1 of 9 (1235 views)
Adding to the beginning of a file Can't Post

Yes, I've read the thread that the FAQ points to, but that thread didn't give me the answer I'm looking for.

What I want to know how to do is how to add content to the beginning of a file, without doing this...


Code
open INF, 'file.txt' or die $!; 
@array = <INF>;
close INF;
open INF, '>file.txt' or die $!;
print INF "new content\n";
print @array;
close INF;

I've always done it that way, but I've learned from the boards that reading files into arrays isn't the best thing to do. So how should I do this?

Jeffrey "muinatit" Lamoureux
muinatit@hotpop.com


sleuth
Enthusiast / Moderator

Feb 16, 2001, 12:00 PM

Post #2 of 9 (1229 views)
Re: Adding to the beginning of a file [In reply to] Can't Post

 
The best thread for this is the first one in the faq, I posted to the thread so it would move to the front, lol.

Tony



muinatit
Deleted

Feb 16, 2001, 1:25 PM

Post #3 of 9 (1226 views)
Re: Adding to the beginning of a file [In reply to] Can't Post

Thanks a bunch. :)

Jeffrey "muinatit" Lamoureux
muinatit@hotpop.com


muinatit
Deleted

Feb 16, 2001, 1:56 PM

Post #4 of 9 (1225 views)
Re: Adding to the beginning of a file [In reply to] Can't Post

Okay I've seen the thread, and the code sample, but I still don't understand how I would use this in one of my scripts. Would somebody who understands this care to explain usage of it?

Jeffrey "muinatit" Lamoureux
muinatit@hotpop.com


japhy
Enthusiast

Feb 20, 2001, 7:38 AM

Post #5 of 9 (1215 views)
Re: Adding to the beginning of a file [In reply to] Can't Post

As I'm the author of that FAQ entry, I'll take this one.


Code
{ 
local ($^I, @ARGV) = ("", $file_to_edit);
while (<>) {
print @new_content if $. == 1;
print;
}
}

That inserts content to the front of a file.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Jean
User


Feb 20, 2001, 9:40 AM

Post #6 of 9 (1214 views)
Re: Adding to the beginning of a file [In reply to] Can't Post

There could be a simpler way to deal with this
(not the most beautiful alg, though it should work):

1. Create a new file (>)
2. Dump your data in the newly created file
3. Open the new file for append (>>)
4. Copy all the strings from the original file into the new one

Hope this helps

Jean
QA Engineer @ http://www.extent.com
mage@lycosmail.com


gregarios
stranger

May 16, 2001, 2:04 PM

Post #7 of 9 (1193 views)
Re: Adding to the beginning of a file [In reply to] Can't Post

I've been looking...
Why exactly is it bad to do it this way?

open INF, 'file.txt' or die $!;
@array = <INF>;
close INF;
open INF, '>file.txt' or die $!;
print INF "new content\n";
print @array;
close INF;



japhy
Enthusiast

May 17, 2001, 1:29 PM

Post #8 of 9 (1173 views)
Re: Adding to the beginning of a file [In reply to] Can't Post

Because it slurps all of the file into an array. That means you have to hold the ENTIRE file in memory. That's silly and probably not needed. It's definitely not a good idea.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


gregarios
stranger

May 17, 2001, 2:07 PM

Post #9 of 9 (1171 views)
Re: Adding to the beginning of a file [In reply to] Can't Post

Would it still be a bad if the size of the file was only about 15K? How about 300K? At what point does it become too cumbersome to use? I need to lift the data from a file once per page view, manipulate a few strings, use all of the data for the web page, then print the modified data back to the file. The actual steps I use are these:

Read file into array.
Manipulate data in array.
Save data back to file and close.
Use ALL the data to form website.

I have to read all the data in anyway to use it in the site. I'm just paranoid or something about overloading the server. Here is the site to give you an idea what it is doing:
www.macpicks.com
The entire site is in a database for easy manipulation. The data is like this in a flatfile:
code|website.com|count|category|name|shortdesc|longdesc|keywds



 
 


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

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