in reply to Re^2: Printing the variable makes the next match code work. Why?
in thread Printing the variable makes the next match code work. Why?

Please note in your node any substantive changes that you make (changing the code is a substantive change).

Actually once the variable name problem was fixed my earlier sample code wasn't so good. However the following revised code works as expected:

use strict; use warnings; my @_lines = map {"$_\n"} split /\n/, <<LINES; ABC in the first line but not the second LINES foreach my $line (@_lines) { my $templine = $line; print "$templine"; # or print "$line"; if ($templine =~ /(ABC)/) {print "$1 = the line (1)\n";} } foreach my $_line (@_lines) { my $templine = $_line; ############ print "$templine"; ############ # or print "$line"; if ($templine =~ /(ABC)/) {print "$1 = the line (2)\n";} }

Prints:

ABC in the first line ABC = the line (1) but not the second ABC = the line (2)

If this is showing your failure mode then I don't understand what the problem is. If it doesn't show your failure mode then modify my sample code until it does and post the updated code along with the "bad" output.

True laziness is hard work

Replies are listed 'Best First'.
Re^4: Printing the variable makes the next match code work. Why?
by scaryfast (Initiate) on Jan 11, 2012 at 05:41 UTC
    Thank You. I see your code is different in a significant way. I found that if I change the print line from ...
    print "$1 = the line"; ##### to #### print "$1 = the \n line";
    So that the print statement includes a '\n' character, it works! And without the print line I previously thought I needed to make it work.