Hi ravi1980,

Welcome to the Monastery, I think this is your first post, please take a look at How (Not) To Ask A Question. Add the code tags for coding part. Your question is not clear and unless your requirement is clear, you cannot get correct answer, you ll get answers only in assumption.

Also use strict and warnings for your coding. In your coding, you are replacing 'apple' with 'APPLE' in which you have used '=' instead of '=~' for regular expressions match. You are using 'e' option modifer unnecessarily in the regular expressions. Your variables are not declared properly in the coding. Take a look at perlre.

Here is my try, if i understood your question correctly,

use strict; use warnings; print "This is test perl program\n"; open ( FILE1,"<1.txt") or die "cannot open $?:$!"; my ($test1, $test2); while (<FILE1>){ 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 (<FILE2>){ 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

Also avoid using $a and $b as variable names because of their use by sort function.

update: Added last para, thanks to wfsp :)

Prasad


In reply to Re: using variable in search and replace by prasadbabu
in thread using variable in search and replace by ravi1980

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.