Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Again, please use <code> tags and read How do I post a question effectively?

I have found strict to be very helpful to identifying and avoiding bugs in my code -- see Use strict warnings and diagnostics or die. If I were going to write your posted code, it might look more like:

use strict; use warnings; open (my $out, ">", "Results.txt") or die ("Could not open file Result +s.txt; $!"); open (my $in, "<", "Textfile.txt") or die ("Can not open input file: $ +!"); local $/; while (<$in>) { if (/(web site.{250})/i) { print $out $1; } }

Changes that I made include:

  1. I swapped to lexical file handles and 3 argument open, which are considered better practice for a number of reasons. See perlopentut. In particular, this gives strict more power to help and removes the need for explicit close.
  2. I corrected inconsistency between your file name and error message; file names are generally case sensitive.
  3. I swapped to slurp mode using $/. Given the large number of characters you are interested in, it is unlikely they will all fall on the same line.
  4. Your while(<>) loop read data into $_ not $ARG, so I corrected that.
  5. I swapped your regular expression to the regular expression I posted above, with the addition of the s modifier. This makes it so . also matches new lines, and is essential when working in slurp mode.

You may consider going to http://learn.perl.org to gather some learning resources before trying to run too far.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re^4: Search and Copy by kennethk
in thread Search and Copy by mikebailey

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 rifling through the Monastery: (3)
As of 2024-03-29 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found