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
 
Hi KT,

You can't edit or delete posts here, if you really need something changing - click on the Red Flag link below your post and explain to site management what you need and why.

WRT your problem.

Could you do two things for me?

Add the following lines to the top of your script and then fix the problems it comes up with:

use strict;
use warnings;

Then post your amended script and a few lines from test.txt for some test data. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Code:
#!/usr/bin/perl

my $file = &quot;test.txt&quot;; #file to edit
my $quan = 1; #limit of $word that the program would edit
my $word = &quot;plain&quot;; #word that I am trying to find
my $rword = &quot;(white)&quot;; #word that I am trying to add behind $word

open(IN,$file) or die &quot;$!&quot;; my @data = <IN>; close IN;
my $data = join('\n',@data);

while ($quan)
{
  last unless $data =~ s/$word/$1$rword/is;
  $quan--;
}

open (OUT,&quot;>$file&quot;) or die &quot;$!&quot;; 
print OUT $data; 
close OUT;

#end
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Thank you. Hm...but what did I do that told the program to erase everything else? Which part did I do wrong? Also, what does &quot;use strict;&quot; mean?
 
It makes you declare your variables before you use them. It would give an error if you just put:
Code:
if($count == 0)
without first declaring in a line above that:
Code:
my $count;
I think the warning switch
Code:
/usr/bin/perl -w
would catch the fact that you haven't yet set a value to $count before testing something against it (if that were the case). ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
For one thing, $realset was never defined. Also, you compared the whole line to only one word, so it could only match if there's only one word on the line. I don't know how that even worked remotely for you. Plus, you used the numerical comparison &quot;==&quot; instead of the string comparison &quot;eq&quot;. What you really want is a search-and-replace regular expression like I showed you.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
O... ok thanks ^_^
Tanderso, the script you posted isn't working perfectly. What it did was it added a \n before every line in the file being edited. However, I fixed the problem. Thank you for answering my questions.

After learning how to program for a new now, I think that debugging is the most stressful and annoying part in programming. And it takes a lot of time. o(><)o

Thank you for reading!
 
Oops, correcting the error in my last post in this thread, in &quot;After learning how to program for a new now&quot; i meant &quot;After learning how to program for a week now&quot;

Sorry for the mistake.
 
Yeah, I should have warned you that I never test any of the solutions I post here, so syntax errors are always possible... in this case, I accidentally put the '\n' in single quotes which doesn't actually interpolate that as a newline... it should have been &quot;\n&quot; in double-quotes instead.

Programming is 90% debugging.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
1 more question:

In
Code:
$data =~ s/$word/$1$rword/is;
what does the &quot;is&quot; do? I remember that there are many more letters that can be used, just like the &quot;is&quot; what are they? What do they do? I tried to find this in my book, but my book never seemed to have mentioned it.

Thank you for reading.
 
Code:
i
means case insensitive, so that in this case &quot;plain&quot; would also match &quot;Plain&quot; or &quot;pLaIn&quot; or any other variation. If you want strict case matching, then remove this part.

Code:
s
means to treat the string ($data) as a single line instead of matching one line at a time seperated by newlines. In other words, a newline (\n) is treated like any other character instead of as a line terminator.

Other regex modifiers include...

Code:
g
means a global match, so that every instance of the left side is replaced by the right side instead of only the first instance.

Code:
e
means that the right side is executable code rather than a string... this is good for modifying the string before replacing it... eg:
Code:
$string =~ s/$html/decode_html($html)/egis;
which might decode the encoded html special characters before replacing it in the string.

Code:
m
means to treat the string as multiple lines so that &quot;^&quot; and &quot;$&quot; match any line in the string instead of only the beginning and the end.

Code:
x
allows you to put comments and whitespace in your regex so that it is more readable.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
O I have a simple question.
I use MS-DOS to run my PERl programs. In a script where it asks for Standard Input for example:

$example=<STDIN>;

If the user just pressed enter, does $example equal to 0?
 
Well, the easiest way to find out is to write a script and try it! It probably returns either null, undef, or a blank string. A good way to handle it if you wanted it to equal zero would be...
Code:
my $example = <STDIN>;
$example = 0 unless $example;
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
No, because STDIN returns a string ,so if you enter nothing, the $example's default value is ''.
 
hm....what does || means?
I know this is a really stupid quesiton, but i seriously don't knwo what it means :(
 
It is a logical &quot;or&quot;. So, if <STDIN> evaluates to false by being blank or null or undef or zero, the &quot;or&quot; gets evaluated instead, so $example = 0 if <STDIN> is false, but $example = <STDIN> if it exists and is true.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
ok, I am trying to write a script that defines $example, if
$example=<STDIN>
and the user just pressed enter

what excatly is $example when that happens?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top