The while (<INF>) only reads one line at a time (where a "line" is defined by the input record separator, $/ which is a newline by default). Since you're only pulling in one line at a time, you'll never match across multiple lines. You need to reset the record separator. For example:
{
# Set the record separator to match the data.
# In this example I have a blank line between
# the lines I want to match, so set $/ to two
# newlines.
# Note that I'm setting $/ in its own data block (the empty
# brackets) to localize the record separator change.
$/ = "\n\n";
while (<DATA>) {
if(/First line of text[\n]second line of text[\n]in the third
+line I have (\d*\.\d*)/s) {
print "Captured ($1)\n";
}
}
}
__DATA__
First line of text
second line of text
in the third line I have 3.14159
Here is another line of text
second line of text
third line has 1.41421356
First line of text
second line of text
in the third line I have 1.73205081
Note that due to the regex, only the first and third numbers will be output.
You could also set the record separator to "First line of text", but keep in mind that you'll need to remove that bit from the regex:
$/ = "First line of text";
while (<DATA>) {
if(/[\n]second line of text[\n]in the third line I have (\d*\.
+\d*)/s) {
print "Captured ($1)\n";
}
}
Added: You could also undef the separator and match globally:
{
$/ = undef;
my $data = <DATA>;
my @array = $data =~ /First line of text[\n]second line of text[\n
+]in the third line I have (\d*\.\d*)/g;
for (@array) {
print "Captured $_\n";
}
}
-
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.