Hi all,
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"; 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"; open (FILE2,"</temp/file2.pl") or die "cannot open $?:$!"; while (<FILE2>){ $i= s/$b/MANGO/e; # This part doesn't work print $_; if($i){ $test2=$_; } } close FILE2; #print "$test2"; ($c,$d)=split(/\s/,$test2,2); print "d= $d\n";

the contents of file1he

ap878ple mango 1apple me45ango a4pple mat5ngo apple mango a2pple ma43ngo a4pple mat5ngo

the contents of file2

mandfgo andwefrwehra maafweewngo andhvwera mango pineapple masvffngo anewfwdhra

Main purpose of the code is given apple I want to search file1 and file2 and find pineapple. the variable $b doesnt seem to work well in the search.

Thanks in advance. Ravi

20060429 Janitored by Corion: Cleaned up formatting, added code tags


In reply to 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.