Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

using variable in search and replace

by ravi1980 (Initiate)
on Apr 29, 2006 at 09:50 UTC ( [id://546477]=perlquestion: print w/replies, xml ) Need Help??

ravi1980 has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: using variable in search and replace
by prasadbabu (Prior) on Apr 29, 2006 at 10:18 UTC

    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

Re: using variable in search and replace
by graff (Chancellor) on Apr 29, 2006 at 16:32 UTC
    When you did this:
    ($a,$b)=split(/\s/,$test1,2); print "b=$b\n";
    you should have noticed the extra blank line in your ouput, after the "b=mango" -- this is because the split is being told to take everything after the first whitespace character (in this case, everything after "apple ") and assign it to $y, and in this case, "everything" includes the line termination character(s) (LF or CRLF, depending on your OS).

    When you look for that target word in the second file, "mango" happens to be in the first column, where it is followed by a space character (not LF or CRLF), so $b never matches, the substitution never happens, and nothing gets assigned to $test2.

    You can either "chomp" the input from the first file before capturing the string you intend to use later, or else you can do  @a = split ' ', $test1; and then use the appropriate element of @a later on.

    (Note that using $a and $b for actual data variables is bad, because if you end up using the "sort" function as well, you'll get messed up. But using @a and @b is okay.)

    UPDATE: Based on your closing comment:

    Main purpose of the code is given apple I want to search file1 and file2 and find pineapple.

    I think a simple script for that would be:

    use strict; my $target; open( I, "file1" ) or die "file1: $!"; while (<I>) { if ( /^apple\s+(.*)/ ) { $target = $1; # note: .* does not match line terminators last; } } close I; my $found; open( I, "file2" ) or die "file2: $!"; while (<I>) { if ( /^$target\s+(.*)/ ) { $found = $1; last; } } close I; print "found: <<$found>>\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://546477]
Approved by gube
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-23 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found