use strict; use warnings; print "This is test perl program\n"; open ( FILE1,"<1.txt") or die "cannot open $?:$!"; my ($test1, $test2); while (){ chomp ($_); my ($i) = $_ =~ s/apple/APPLE/; #just replacing apple with APPLE #this works fine if($i) { $test1=$_; } } close FILE1; print "test1 = $test1\n\n"; #this has the the whole line which is of #the form a b my ($a,$b)=split(/\s/,$test1,2); print "b = $b\n"; open (FILE2, "<2.txt") or die "cannot open $?:$!"; while (){ chomp($_); my ($i) = $_ =~ s/$b/MANGO/; if($i) { $test2=$_; } } close FILE2; print "\ntest2 = $test2\n\n"; my ($c,$d)=split(/\s/,$test2, 2); print "d = $d\n"; outputs: -------- This is test perl program test1 = APPLE mango b = mango test2 = MANGO pineapple d = pineapple