Need Help With Perl...

They have: 117 posts

Joined: Mar 2000

Can anyone tell me how to edit a single line of a file? I'm programming in Perl. Thanks!

--Edge

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Do you have any experience with Perl?
Do you have any experience with regex?

Basically, read the file into a $var. Then use
$var = s/my_pattern/my_replacement/;
where 'my_pattern' is a regex pattern that you are looking for, and 'my_replacement' is what you want to replace it with.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 117 posts

Joined: Mar 2000

That's what I'm doing, but one part of my line has a question mark in it and somehow it's getting messed up when it replaces it. Is there something I should know about question marks??

--Edge

tmay's picture

They have: 75 posts

Joined: Sep 2001

Can you post the regex you are using now? Question marks are used in Perl for matching. I think Perl is reading it wrong depending on how you have it used.

-Troy May

PC Sympathy

Admin at HFT Online

tmay's picture

They have: 75 posts

Joined: Sep 2001

Actually, I'll try to save you a step: Just try putting a backslash (\) right in front of the question mark and see if that works now. If that don't work, then post the regex with an example line of the file you are trying to edit.

-Troy May

PC Sympathy

Admin at HFT Online

They have: 117 posts

Joined: Mar 2000

Yeah, I tried the \? thing and it solved it. But, the problem with that was that when the script reads the file and displays it through my browser, it still shows the slash in from of the question mark.

--Edge

tmay's picture

They have: 75 posts

Joined: Sep 2001

Can you post the code you are using? Or a link to the page?

They have: 117 posts

Joined: Mar 2000

Okay, sorry for the slow reply, but forget the editing a particular line for a second. I'm having more trouble deleting a particular line. Here's the code I'm using:

$file = "$data_path\\news.txt";
$line = $FORM{'id'};

open(IN,"<$file");
@array = <IN>;
close(IN);

$num = 0;

for (@array) {
if($_ =~ /$line/) {
splice(@array,$num,1);
}
$num++;
}

open(IN,">$file");
print IN @array;
close(IN);
'

That works great except when I tell it to delete line 1. In other words, $FORM{'id'} = 1. When do that, it goes and deletes every other line in the file. Otherwise, it's working perfectly.

--Edge

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Quickie Code (untested)

$file = "$data_path\\news.txt";
$line = $FORM{'id'};

open(IN,"<$file");
@array = <IN>;
close(IN);

$num_lines = @array;
open(OUT,">$file");
for ($i=0; $i<$num_lines; $i++) {
    if($i != $line) {
        print OUT $array[$i];
    }
}
close(OUT);
'

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 117 posts

Joined: Mar 2000

Hey, it worked!!
I just had to add
$line--;'
after

$file = "$data_path\\news.txt";
$line = $FORM{'id'};
'

Thanks!!!!

--Edge

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.