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:
Appening to EOF

 



xMattHawkx
stranger

Jun 18, 2001, 10:02 AM

Post #1 of 7 (2791 views)
Appening to EOF Can't Post

Hi again,

I'm trying to append to the end of a file, but I do NOT want to do this:

open (FILE, ">>$filename")
close (FILE);

I've been trying to seek the line number of the last line of the file and then print a the new line in using inplace editing. I must be doing something wrong. Take a look:

This is seeking the last line number, and this works...

open (FH, "$filename") or die "can't open $filename: $!";
while ( <FH> ) {
$addr = $. unless eof(FH);
}
close (FH);

Now, I get to adding the new line in:

{
local $^I = ".bak"; # the backup extension
local @ARGV = "$filename"; # the file to edit
while (<>) {
print;
print "Testing 123\n" if $. == $addr;
}
}

It adds the line, but not on the last line of the file, instead in prints on the second to last line. And finally...

Here's my question:

Is there a way to force it to print on the next line?

Thanks for your time!

xMattHawkx



Mortimer
journeyman

Jun 19, 2001, 7:02 AM

Post #2 of 7 (2771 views)
Re: Appening to EOF [In reply to] Can't Post

>This is seeking the last line number, and this works...

>$addr = $. unless eof(FH);

No it doesn't. This will assign the penultimate value of $. to $addr because the invisible eof character resides on the last line. You can say...


Code
$addr = $. if eof(FH);

...or not bother at all...


Code
$addr = $.;

If you use the unless, your inplace edit will place the line before the last line in your file. You also have the lone print statement in the wrong place. But anyway, there's no need for the prior open of your file, you can do the lot in your inplace edit. Your code adjusted...


Code
local $^I = ".bak"; # the backup extension 
local @ARGV = "$filename"; # the file to edit
while(<>){
s/$_/Testing 123\n/ if eof;
print;
}

Ugly init?

Cheers,
Dave.
www.dmscripts.com
davemortimer@bigpond.com



(This post was edited by Mortimer on Jun 19, 2001, 6:09 AM)


xMattHawkx
stranger

Jun 19, 2001, 2:58 PM

Post #3 of 7 (2761 views)
Re: Appening to EOF [In reply to] Can't Post

Cool, that works. Thanks for explaining it to me :).

____New_______>>

Hehe...it worked on my small test file. But once I used it on my real file, it added it on the second to last line again...hmm.

(This post was edited by xMattHawkx on Jun 19, 2001, 2:20 PM)


xMattHawkx
stranger

Jun 19, 2001, 5:34 PM

Post #4 of 7 (2756 views)
Re: Appening to EOF [In reply to] Can't Post

I diagnosed the problem, and here's whats happening:

This is the last line in the file:

25|name|email|house|None|None|Student|50|20|0|None|None|None|0|None|date

Now when I run the script:

25|name|email|house|None|None|Student|50|20|0|None|None|None|0|None|date
|name|email|house|None|None|Student|50|20|0|None|None|None|0|None|date

Notice how the new last line is missing its first field (id number)?

Then, if the code is used yet again (and every time thereafter):

25|name|email|house|None|None|Student|50|20|0|None|None|None|0|None|date
27|name|email|house|None|None|Student|50|20|0|None|None|None|0|None|date
|name|email|house|None|None|Student|50|20|0|None|None|None|0|None|date

See how the new line (id 27) prints on the second to last line?

I'm quite boggled by this O.o. Any help/advice is of great appreciation. :)



Mortimer
journeyman

Jun 20, 2001, 1:50 AM

Post #5 of 7 (2753 views)
Re: Appening to EOF [In reply to] Can't Post

Sorry xMattHawkx, I forgot to mention that the eof pixie lives at the beginning of the last line in the file, and his name is 26. Try this...


Code
s/(.*)/$1\n26\|Testing 123/ if eof;

But >> should really do this job.

Cheers,
Dave.
www.dmscripts.com
davemortimer@bigpond.com




xMattHawkx
stranger

Jun 20, 2001, 6:18 AM

Post #6 of 7 (2746 views)
Re: Appening to EOF [In reply to] Can't Post

Alright! That works! Positive this time :p.

Thank you very much for helping my out :).

If you need help with your site (graphics/page design) just lemme know, that's my real specialty.



patrik
stranger

Jul 8, 2001, 1:34 AM

Post #7 of 7 (2730 views)
Re: Appening to EOF [In reply to] Can't Post

Or U can put a extra | at the end of every line, works great for me

Ex.
25|name|email|house|None|None|Student|50|
26|name|email|house|None|None|Student|50|
27|name|email|house|None|None|Student|50|

Donīt know if thats a goog ide thou, but it works =)
HTH
Patrik

Excuse the bad english
Iīm from Sweden ;)

 
 


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

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