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:
Sachin
Ganguly
Dravid
Laxman
Sehwag
after the insertion, I want it to look like this:
Sachin
Ganguly
foo
Dravid
Laxman
Sehwag
I came up with this 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:
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?