CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
General Substitute Function

 



sankoh
newbie

Oct 31, 2001, 11:32 AM

Post #1 of 1 (3493 views)
General Substitute Function Can't Post

Hello all,

I wrote a function that takes a SEARCH_STRING, and replace it with NEW_PARAMETER. If I use parenthesis in some portion of SEARCH_STRING, those portion in parenthesis should be be saved in special read-only variables.($1, $2, $3, etc). When I tried to run this code, I get " Use of Uninitialized value at ./substitute.pl. line 6 " . I think the problem is passing the $1 into function. My intention is for this $1 to represent all characters in first parenthesis. Below is the code that was executed. Please help if you know answer to my question. Thanks in advance

################################MAIN#########################################
substitute("^(somePattern).*","$1 = 1;","./testFile");

################################FUNCTION#####################################

sub substitute {
my ($SEARCH_STRING,$NEW_PARAMETER,$FILE_NAME)= @_;
my ($TMP_FILE_NAME);

$TMP_FILE_NAME = "$FILE_NAME".".TMP".".1";

#open necessary files
open (LINE, "$FILE_NAME") || die "Unable to open $FILE_NAME: $!";
open (TMP, ">$TMP_FILE_NAME") || die "Unable to open $TMP_FILE_NAME: $!;

#Loop through file make substitution and send to temp file
while ($ENTRY = <LINE>) {
$ENTRY =~ s/$SEARCH_STRING/$NEW_PARAMETER/;
print TMP "$ENTRY";
}
}

testFile

somePattern = 0;

After execution of script testFile.TMP.1 should be:

somePattern = 1;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives