Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi, you have a working solution by now, but just a few comments on some mistakes in your code, to help you progressing:
while (<F1>) { open (F3, "<new3.txt") or die; my $lines = <F3>; print F2 if (/$lines/); close (F3); }

First it is a very bad idea to open the same file repeatedly within a while loop. This is very inefficient. Whenever you have something like that, read the file with the elements to be found prior to the main loop and store these items in memory (an array, a hash, a regex, a string, something, whatever).

Second, when you read F3, you need to chomp the line to remove the trailing end of line. Else you will be looking for "c_c\n", not "c_c", so that you won't find it.

Third, this code line:

my $lines = <F3>;
is reading only one line from the file (the first one). So that if the other errors were corrected, you would still be able to catch only the first intemof the file.

Finally, this is not an error, your code will work on that, but it is considered better practice to use the so-called three-argument syntax for opening a file and to use lexical file handles:

open my $SEARCH_ITEMS, "<", "new3.txt") or die "Could not open new +3.txt $!";; my @lines = <$SEARCH_ITEMS>;
Note that I also added a message to the die statement: 1. saying what the problem is ("Could not open..."), 2. telling which file the script could not open, and 3. displaying the error message reported by the OS (the $! special variable). Believe me, this helps when you have scripts opening dozens of files.

I hope this helps.

Update: Corrected a couple of typos and added a little bit of further explanations.

Je suis Charlie.

In reply to Re: How to read the regular expression from another file? by Laurent_R
in thread How to read the regular expression from another file? by sumathigokul

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 scrutinizing the Monastery: (8)
As of 2024-04-19 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found