Re: using variable in search and replace



swamy455@xxxxxxxxx wrote:
I am trying to search for a string in FILE1 and then split it and then
use the result to search FILE2

#!/usr/bin/perl
print "This is test perl program\n";

print "so of course I enable strictures and warnings:\n";
use strict;
use warnings;

open ( FILE1,"</test/file1.pl") or die "cannot open $?:$!";
while (<FILE1>){
$i= s/apple/APPLE/;
#just replacing apple with APPLE
#this works fine
if($i){
$test1=$_;
}
}
close FILE1;
print "test1 =$test1\n";
#this has the the whole line which is of #the form a b

($a,$b)=split(/\s/,$test1,2);
print "b=$b\n";

Replace that line with

print "b=[$b]\n";

and consider the result.

open (FILE2,"</temp/file2.pl") or die "cannot open $?:$!";
while (<FILE2>){
$i= s/$b/MANGO/e;
--------
Enable strictures, and avoid such mistakes!

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
.



Relevant Pages