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