Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Putting a limit on find and replace 1

Status
Not open for further replies.

programingnoob

Technical User
Dec 31, 2002
71
CA
Hi,
I have come for help again, seeing that so many of you people are so nice and helpful :)

My program is suppose to find a word and add something behind it, which it does. After succeeding, I have try to put a limit on how many words my program will replace. My program did replace the words, but the words that it didn't replace is deleted! :(

What is wrong with my program? How can I correct it?

Here's my program --------------------------------------

#!/usr/bin/perl
$file=test.txt; #file to edit
$quan=1; #limit of $word that the program would edit
$word="plain"; #word that I am trying to find
$rword="(white)"; #word that I am trying to add behind $word

$num=0;
open IN, $file;
while ($ln=<IN>) {

if ($ln==$word and $quan>0) {
$ln=qq|$word $realset|;
$quan--;
} else {
$ln=$ln;
}

$setedit[$num]=$ln;
$num = $num +1;
}
close IN;

open OUT, &quot;>$file&quot;;
print OUT @setedit;
close OUT;

#end
---------------------------------------------------------

Also, how do I delete or edit posts in Tek-tips?

Have a nice day !
KT
 
tl66 says that it would be a blank string. If you use my method above, you can use the || to make it equal something else (eg. 0) if it is blank.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top