Editing A Specific Line?

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,048 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.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.