Ezilon.com - Target Your Audience, be Seen in Your Region

Editing A Specific Line?

You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

They have: 117 posts

Joined: Mar 2000

How would I go about changing a specific line in a file? Or just deleting a line and re-printing one in the same spot?

Thanks!

--Edge

They have: 568 posts

Joined: Nov 1999

$file = "something.txt";
$line = 4;

open(file,$file);
@contents = <file>;
close(file);

$contents[$line] = "something new\n";

'

this will change line 4 of something.txt to the string "something new" and a line break at the end.

They have: 117 posts

Joined: Mar 2000

Your code changed the line after the one I wanted it to, so I had to add $line--; and then it worked. Thanks!!

--Edge

Mark Hensler's picture

He has: 4,044 posts

Joined: Aug 2000

that's because arrays start with 0
so the first line would be $contents[0]

They have: 568 posts

Joined: Nov 1999

oh *smacks himself* forgot to mention that Smiling

and the fact that you need to re-write the array to the file your opening.