Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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";

In reply to Re: using variable in search and replace by graff
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found