Hello Carrie, It can be frustrating to get the Perl syntax down, but it is such a great language and powerful tool, I highly recommend you push on! I have modified your script a bit so that it works the way you are expecting it to (well for the most part). I have noted some of the changes and commented out the lines of yours that I changes/replaced.
Shawn #!/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 #----------------------------------------------------------------- # Use strict ALWAYS to save you headaches down the road! use strict; # 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(my $file = <STDIN>); # open the user inputed file or cancel the program if it is incorrect #open (FILE, ">>$file") || die "Could not open $file! $! \n"; # You were opening the file to append to it with '>>', here I open it for # read only '<' and then open a second file to write your results to '>'. # '>' will create a new file if needed or write over an existing one. open (INFILE, "<$file") || die "Could not open $file! $! \n"; open (OUTFILE, ">$file.new") || die "Could not open $file.new! $! \n"; # ask user what text to search for print "What text would you like to search for?\n"; chomp(my $text = <STDIN>); # ask user for the replace text print "What text would you like to replace $text with?\n"; chomp(my $replacetext = <STDIN>); # set a variable to 0 for a counter my $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){ # I just threw the $contents into the while loop directly. while (my $contents = <INFILE>){ # Test to see if this line contains the test we are looking for if ($contents=~/$text/i) { # Show the line that is matched and ask if you want to continue print "$text found in:\n $contents"; print "Would you like to replace it?\nType yes or no\n"; chomp(my $answer = <STDIN>); # I added the option of 'yes' or 'y' # Here you were setting $answer equal to "yes". You want to use the # 'eq' operator for string matches and '==' for numeric matches to # check for equality. # unless ($answer = "yes"){ unless ($answer eq "yes" || $answer eq "y"){ die "Program will not continue per your request\n"; } else { # Here we actually do the replacement and increment the counter $contents =~ s/$text/$replacetext/gi; $counter++; } # $contents = <FILE>; } # Here were write the output to the new file. This will write to the # new file if there was no match, or you said yes above. print OUTFILE $contents; } # print how many matches/replacements the program made print "The program is complete. It replaced $counter matches\n"; # close the file (Both the infile and outfiles close (INFILE); close (OUTFILE); # exit the script, not needed, but good practice exit; ----- Original Message ----- From: "Carrie Lyn Brammer" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Friday, July 19, 2002 3:46 AM 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]