Maybe, you can try simplier test at beginning... it 's diffictuly for you to
track down what's wrong with your code, since you still not yet turn on
strict
and warnings. maybe typing error, maybe syntax error, you don't know unless
you use strict, and use warnings.

Your example is including <STDIN>, chomp, open and read file, loop, and
regex.
If you just want to learn regex, try something simpiler, like this :

$text = "I'm stuck again. I'm trying to write a search/replace program,
now.";
$target = "n";
$replacement = "N";

$text =~ s/$target/$replacement/ig;
print $text;

so you got : $ text = "I'm stuck agaiN. I'm tryiNg to write a search/replace
program, Now"
All the n changes to capital letter N.

For a better version, you may doing like this :
01 use strict;
02 use warnings;
03
04 my $text = "I'm stuck again. I'm trying to write a search/replace
program, now.";
05 my $target = "n";
06 my $replacement = "N";
07
08 $text =~ s/$target/$replacemen/ig;
09 print $text;

So I got this error warning !!
Global symbol "$replacemen" requires explicit package name at sample.pl line
8.
Execution of sample.pl aborted due to compilation errors.

Let's see, I've type the $replacement wrongly as $replacemen. (missed the
't'),
inside strict, any variable much declare with 'my', 'our', or 'local' when
the first
time it uses. and when next time, you will have no chance to type this
wrongly.
because a mistyped var name had not be declared, so you will get the warning
like
above. So 08 should correct as $text =~ s/$target/$replacement/ig; and the
result
goes fine. That's a snap shoot for the benifits for use strict and warnings.

Rgds,
Connie

PS. Don't give up on Perl, that's one of the most interesting stuff I digged
out from
this world =)


----- Original Message -----
From: "Carrie Lyn Brammer" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, July 19, 2002 4:46 PM
Subject: Regular Expression Problems?


Hello All,

I'm stuck again. I'm trying to write a search/replace program, now. But for
some reason, it is not working.  I'm about ready to give up learning perl.
It seems like every project in this book, I have trouble with...and I can't
do my own troubleshooting.  So anyway, if one of you kind souls could please
take a quick glance at the script below, I would appreciate the feedback. I
was thinking, perhaps its a problem with the regular expression and how the
scalar variables are presented... I dunno *shrugs*:

#!/usr/bin/perl
#-----------------------------------------------------------------
# Script Name: Project7-2.pl from Perl: A beginners guide
# Script Version: 1.0
# Date: July 18th 2002
# Author: Carrie Lyn Brammer
# Description: A find/replace application ---> A Program that mimics the
action of the find/replace option
# available on most word processors. It should first select the file you
want to work with by entering the
# total path to the file as user input. Once the file is specified, ask for
a search item and a replace item
# to be entered from the command line. Once it has these two pieces of
information, it should perform
# the search for the regular expression through the file and whenever it
makes a match, perform the
# replacement. After each replacement is made, it should prompt the user to
see if he or she wants to
# continue When the end of the file is reached,  it should notify the user
that the find/replace is complete
# and print the number of replacements made out to the screen.
# Revision History:
#     1.0/<July 18th 2002>: original version
#-----------------------------------------------------------------

# ask user for a file name
print "Type the file name you would like to open:\n";

# set user input to variable and remove newline character
chomp($file = <STDIN>);

# open the user inputed file or cancel the program if it is incorrect
open (FILE, ">>$file") || die "Could not open $file!  $! \n";

# ask user what text to search for
print "What text would you like to search for?\n";

chomp($text = <STDIN>);

# ask user for the replace text
print "What text would you like to replace $text with?\n";

chomp($replacetext = <STDIN>);

# set a variable to 0 for a counter
$counter = 0;

# set a line of text to a variable
$contents = <FILE>;

# a loop that will go through each line of the document. Implement the user
input into  a regular expression
# order to search and replace. If a replacement is made, it should ask the
user if they want to continue.
# If they don't the program will close. If they do, it will continue
searching the rest of the file.
while ($contents =~ s/$text/$replacetext/gi){
    print "Made a replacement per your request. Would you like to continue?
Type yes or no \n";
    chomp($answer = <STDIN>);
        unless ($answer = "yes"){
            die "Program will not continue per your request\n";
        }
    $contents = <FILE>;
    $counter++;
}

# print how many matches/replacements the program made
print "The program is complete. It replaced $counter matches\n";

# close the file
close (FILE);

Thank you,
Carrie Lyn



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to