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: Beginner:
add line in the top of the file

 



widexl
Novice

Mar 17, 2001, 3:19 PM

Post #1 of 8 (853 views)
add line in the top of the file Can't Post

Now someone the way to add a line in the top of the database (flat line).

Thank



Jasmine
Administrator / Moderator

Mar 17, 2001, 3:49 PM

Post #2 of 8 (849 views)
Re: add line in the top of the file [In reply to] Can't Post

(Post moved from Advanced to Beginner forums)

This question is in the FAQ Inserting Lines Into Files





widexl
Novice

Mar 17, 2001, 4:11 PM

Post #3 of 8 (847 views)
Re: add line in the top of the file [In reply to] Can't Post

Is thit the way to do it....

open FILE, ">> $path_to_file";
if ($. == 0) {
print FILE "new line\n";
}
close FILE;

I just want to put the lines in the top, so it's easy to view the last singups in the script



widexl
Novice

Mar 21, 2001, 9:34 AM

Post #4 of 8 (827 views)
Re: add line in the top of the file [In reply to] Can't Post

I have try the things in the FAQ.
But it is not working



japhy
Enthusiast

Mar 21, 2001, 10:40 AM

Post #5 of 8 (826 views)
Re: add line in the top of the file [In reply to] Can't Post

The following code, FROM THE FAQ, works for me:


Code
{ 
local $^I = '.bak';
local @ARGV = "filename";
while (<>) {
print $new_stuff if $. == 1;
# EDIT
print; # forgot this line -- this prints the original line
}
}

Done.

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

(This post was edited by japhy on Mar 21, 2001, 10:32 AM)


widexl
Novice

Mar 21, 2001, 11:09 AM

Post #6 of 8 (824 views)
Re: add line in the top of the file [In reply to] Can't Post

Thanks Japhy

In the post "Inserting Lines Into Files"

you are writhing

local @ARGV = $path_to_file; # the file to edit

But so far I now is then @ARGV = /path/to/the/file
And not what is in the file.

Do I need first to read the file, put everything in the @ARGV
and then open it and write the @ARGV in the file.
that the = "filename"; is the file when I have read it.

I have try a lot off things And I can't figger it out (the first time).
Henk



japhy
Enthusiast

Mar 21, 2001, 11:32 AM

Post #7 of 8 (823 views)
Re: add line in the top of the file [In reply to] Can't Post

All you need to put in @ARGV are the file names you want to work with. Perl takes care of all the work for you. You just tell it where to make modifications:


Code
{ 
local $^I = '.bak';
local @ARGV = ("/foo/bar/this.pl", "/foo/blah/that.pl");
while (<>) {
print "#!/usr/bin/perl -w\n" if $. == 1;
print;
}
}

That will insert a line at the top of the files.

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


widexl
Novice

Mar 23, 2001, 3:32 PM

Post #8 of 8 (809 views)
Re: add line in the top of the file [In reply to] Can't Post

Hello Japhy
I have try what you say. And it's working on some way.
But the problem is that when I run the script.
It deletes all the data what is in the file.
And then ad just an line.
and in the *.bak comes the old data.
Is this normal???

{
local $^I = ".bak";
local @ARGV = ("$path_to_file");
while (<>) {
print "new_line_test\n" if $. == 1;
}
}

I have try something else and that is working.
But this slow down the script because the database need to update the hole database.

# the database what need to be updated
$path_to_file = "/opt/guide/www.widexl.com/cgi-bin/test/test.txt";

# new database
$path_to_file_new = "/opt/guide/www.widexl.com/cgi-bin/test/test.new.txt";

print "Content-type: text/html\n\n";

# this is working for but makes the script slow
open FILE, "<$path_to_file";
@data_base = <FILE>;
close (FILE);

open open(NEW, ">$path_to_file_new");
print NEW "new_line\n";
foreach $item(@data_base) {
chomp($item);
print NEW "$item\n";
}
close (NEW);

But this is working for my.

some help here is nise.
Henk

 
 


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

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