I assumed that, $1 would be reset (either undefined or set to null) once I exited the scope of the while loop my regexp was called in. Sadly, I was mistaken. :) Below is a test example of code I wrote, with $1 values differing from those I expected. Do I need to explicitly set $1..$n to an empty string before every regexp if I'm to test based on the results of that regexp?
No you don't explicitly set or unset these variables, you must only use them if the regex succeeded.
In fact these variables are read-only (perldoc perlvar)
Also read through
perldoc perlretut
perldoc perlre
Thanks! - Ed
-=-=-=-=-=- #!/usr/bin/perl -w use strict; use warnings;
while (<DATA>) { chomp; /(\d+)/; print "\$1: $1\n";
Change the above two lines to if (/^(\d+)$/) { print "\$1: $1\n"; } else { print "Unexpected...\n"; }
It is also a good idea to add anchors to your regexes
} __DATA__ 1 2 a 3
Expected results: $1: 1 $1: 2 $1: # <--- was expecting an empty string $1: 3
Actual results: $1: 1 $1: 2 $1: 2 # <--- value of $1 never reset from the last run $1: 3
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]