in reply to Printing the variable makes the next match code work. Why?

Are you sure the first one works and the second doesn't? If I add strictures to the top of your script and supply @_lines the following error is generated:

Global symbol "$_line" requires explicit package name at noname1.pl li +ne 10. Execution of noname1.pl aborted due to compilation errors.

Line 10 is the line my $templine = $_line; in the first loop. If I remove strictures but add (1) and (2) as appropriate to the conditional print statements in the two loops I get:

ABC = the line (2)

so maybe all your problem is is that you aren't using strictures and you have an old fashioned "typo in a variable name" error? Oh, the test code I used was:

#use strict; #use warnings; my @_lines = <<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)";} } foreach my $_line (@_lines) { my $templine = $_line; ############ print "$templine"; ############ # or print "$line"; if ($templine =~ /(ABC)/) {print "$1 = the line (2)";} }

Uncomment the strictures to get helpful errors and warnings.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Printing the variable makes the next match code work. Why?
by scaryfast (Initiate) on Jan 11, 2012 at 04:58 UTC
    Thanks My Bad. I am sorry. use strict; is in my code I fixed the typos( all $_line should have said $line) in the code but you had already responded. I don't want to have to print to screen because then I can't easily see my output. Please help me again, even though I did not post well. :) Thanks

      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
        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.