Jorge Goncalves wrote:
Hi List , I have this to print:
1
2
3

with this script but it didn't work:

No, it does not even compile. :(

while ($file =~ /^\s+(\d+).(.+)$/) {

You always need the /g modifier when using the m// operator that way in a while loop. In this case you also need the /m modifier. Check out


    perldoc perlop

about what those modifiers are doing.

    while ($file =~ /^\s+(\d+).(.+)$/gm) {

print $1\n if $2 =~ /extractStat/;

Let's first correct the compilation error (something you should have done before posting, btw).


        print "$1\n" if $2 =~ /extractStat/;

But now the script just outputs "uninitialized" warnings.

It's time to consider the program logic. $1, $2 etc. contain what was captured in the last successful match. This would make the program logic appear more clearly:

        if ($2 =~ /extractStat/) {
            print "$1\n";
        }

The point with that is to show that when the program comes to print "$1\n", the last successful match is no longer the m// operator in the while loop, but it's $2 =~ /extractStat/. Since nothing was captured there, $1 is undefined, which explains the warning messages.

This is one way to deal with it:

        my $tmp = $1;
        print "$tmp\n" if $2 =~ /extractStat/;

HTH

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to