in reply to Re: Two lines Regular Expression
in thread Two lines Regular Expression

Thanks that works now how do I get that match to print outside of the while loop?? I tried something using $data but no luck:
open(TMPFILE,"<main.html"); while (<TMPFILE>) { chomp; my $data; my $in = join('',<TMPFILE>); if ($in =~ /(myword)\s+nextLine/) { print "Match = " . $1; $data = $1; } } print $data #I want this to print my match results here on myword # so it should print "myword" here but it doesnt.

Replies are listed 'Best First'.
Re: Re: Re: Two lines Regular Expression
by elvester (Beadle) on Jun 09, 2003 at 21:42 UTC
    It looks like your $data is out of scope. I would recommend use strict; it would have given you an error. Move the my $data; before the the while loop if you want to print afterwards.
      Thanks. Please advise how this catches the next line?
      ($_ .= <TMPLFILE>;)
      what and how does it do that???

        It works by concatenating (with ".") the contents of $_ (your current line in this case) with the last line(s) read in. It is equivalent to: $_ = $_ . <YOURFILE>. You can have a look at perldoc perlop under "Assignment Operators" for a more detailed explanation.

        If you have:

        line one line two line three

        the first time around, $_ is "line one\n", the second time around, $_ becomes "line one\nline two\n", and the third time around, $_ becomes "line one\nline two\nline three\n", which allows you to use your regex across more than one line in the file.

        --
        Allolex

        That just uses a special kind of string concatanation operator to append the input. It's equvilent to $_ = $_ . <TMPLFILE>;

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        Note: All code is untested, unless otherwise stated