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:
Anomaly while opening a file in read-write mode

 



Cupidvogel
Novice

Mar 24, 2012, 8:00 AM

Post #1 of 5 (1240 views)
Anomaly while opening a file in read-write mode Can't Post

Hi, I have a file which contains some lines of text. What I want to do is after the first two lines, insert a new sentence. So if the original file looks something like this:


Code
Sachin 
Ganguly
Dravid
Laxman
Sehwag


after the insertion, I want it to look like this:


Code
Sachin 
Ganguly
foo
Dravid
Laxman
Sehwag


I came up with this code:


Code
open f, "+< t.txt"; 
while (<f>) {
if (1..2) { print; }
else { last; }
}
print f "foo";
close f;


but when I run it, instead of the desired output, I get this:


Code
Sachin 
Ganguly
Dravid
Laxman
SehwagSachin
Ganguly
Dravid
foo


Notice that the first three lines have been appended to the last line, after which foo is printed out. Can anyone point out where am In erring, and what might be the correct way?


BillKSmith
Veteran

Mar 24, 2012, 8:51 AM

Post #2 of 5 (1237 views)
Re: [Cupidvogel] Anomaly while opening a file in read-write mode [In reply to] Can't Post

It really is not possible to do exactly what you are trying to do. The easiest way to make changes in the middle of a file is to use the perl run-time option -i (refer perldoc perlrun).
Good Luck,
Bill


Cupidvogel
Novice

Mar 24, 2012, 8:56 AM

Post #3 of 5 (1236 views)
Re: [BillKSmith] Anomaly while opening a file in read-write mode [In reply to] Can't Post

Can you dissect the logic of the code to say where it is gowing wrong? The plus sign enables both reading and writing, right? After reading two lines (by implicitly comparing $.), the file pointer should be at the starting of 3rd line, right ? (In fact, just for cross-checking, if the next line after the loop is print <f> the 3rd line is indeed printed out. So no issue there too) Now that the file pointer is at the beginning of the 3rd line, why can't I write something at that position? More importantly, how the code is yielding the current output?


(This post was edited by Cupidvogel on Mar 24, 2012, 8:57 AM)


FishMonger
Veteran / Moderator

Mar 24, 2012, 12:09 PM

Post #4 of 5 (1232 views)
Re: [Cupidvogel] Anomaly while opening a file in read-write mode [In reply to] Can't Post

I have a few minutes before my diving, so I'll be brief.

Opening the file in read/write mode is almost always the wrong approach and won't do what you want because it overwrites the contents.

As I mentioned in your related devshed post, you can use the Tie::File module and combine that with the splice function to insert the new line(s).

See: perldoc -f splice
http://perldoc.perl.org/functions/splice.html


Cupidvogel
Novice

Mar 24, 2012, 12:11 PM

Post #5 of 5 (1230 views)
Re: [FishMonger] Anomaly while opening a file in read-write mode [In reply to] Can't Post

Ok. Will do.

 
 


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

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