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:
Substitute?

 



Tigol
Novice

Mar 15, 2005, 4:12 PM

Post #1 of 4 (511 views)
Substitute? Can't Post

Hello again,

Could someone please give me a hand with this code? I'm trying to parse a text file for any instances of "Place Holder" and replace it with "Comment". The below code finds the string but removes it completely from the backup file it creates. Thanks again in advance.

print("opening file $rgp");
open(RGPFILE, $rgp) ||die "Couldn't open $path for writing: $!\n";
open(RGPFILENEW,"> $rgp.backup") ||die "Couldn't open $path for writing: $!\n";
while($line = <RGPFILE>)
{
if($line =~ "Place Holder")
{
s/Place Holder/Comment/g;
}
else
{
print RGPFILENEW $line;
}
}
close RGPFILE;
close RGPFILENEW;


Tigol
Novice

Mar 15, 2005, 4:55 PM

Post #2 of 4 (507 views)
Re: [Tigol] Substitute? [In reply to] Can't Post

Nevermind. I figured it out. Smile


KevinR
Veteran


Mar 15, 2005, 5:00 PM

Post #3 of 4 (507 views)
Re: [Tigol] Substitute? [In reply to] Can't Post


Code
if($line =~ /Place Holder/) 
{
$line =~ s/Place Holder/Comment/g;
}


or you could write the while loop like this:


Code
 while(<RGPFILE>) 
{
if(/Place Holder/)
{
s/Place Holder/Comment/g;
}
print RGPFILENEW $_;
}


I don't think you need the else {} block in the code for what you are doing.
-------------------------------------------------


MrPJ
User

Mar 15, 2005, 5:46 PM

Post #4 of 4 (505 views)
Re: [KevinR] Substitute? [In reply to] Can't Post


Code
print RGPFILENEW $_;


print RGPFILENEW;

Cool

 
 


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

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