note
kennethk
<p>Again, please use <c><code></c> tags and read [id://174051]
<p>I have found [doc://strict] to be very helpful to identifying and avoiding bugs in my code -- see [id://87628]. If I were going to write your posted code, it might look more like:
<c>use strict;
use warnings;
open (my $out, ">", "Results.txt") or die ("Could not open file Results.txt; $!");
open (my $in, "<", "Textfile.txt") or die ("Can not open input file: $!");
local $/;
while (<$in>) {
if (/(web site.{250})/i) {
print $out $1;
}
}
</c>
<p>Changes that I made include:
<ol>
<li>I swapped to lexical file handles and 3 argument open, which are considered better practice for a number of reasons. See [doc://perlopentut]. In particular, this gives [doc://strict] more power to help and removes the need for explicit [doc://close].
<li>I corrected inconsistency between your file name and error message; file names are generally case sensitive.
<li>I swapped to slurp mode using [doc://perlvar#$/]. Given the large number of characters you are interested in, it is unlikely they will all fall on the same line.
<li>Your <c>while(<>)</c> loop read data into [doc://perlvar#$_] not <c>$ARG</c>, so I corrected that.
<li>I swapped your regular expression to the [doc://perlretut|regular expression] I posted above, with the addition of the <c>s</c> modifier. This makes it so <c>.</c> also matches new lines, and is essential when working in slurp mode.
</ol>
<p>You may consider going to [http://learn.perl.org] to gather some learning resources before trying to run too far.
<!-- Node text goes above. Div tags should contain sig only -->
<div class="pmsig"><div class="pmsig-712372">
<hr />
<p>#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.</p>
</div></div>
1005278
1005298