in reply to $1 doesn't reset?
The problem is that the "for" loop does all the matching beforehand. the stuff inside the bracket is evaled in an array context before the loop starts. You could also write it like this:
@allmatches = $text =~ m/foo(.*?)bar/g; for ( @allmatches ) { print "dollarunderscore= $_ \tdollarone= $1\n"; }
$_ is set to the stuff you matches, $1 is always the last match - just as you described in your post.
This is quite different from the while loop. Here the expression in brackets is evalutated in a scalar context, which means the matching is done one at a time:
while ($text =~ m/foo(.*?)bar/g) { print "dollarunderscore= $_ \tdollarone= $1\n"; }
You could also write this as
</code>$text =~ m/foo(.*?)bar/g; print "$_ \t$1\n"; $text =~ m/foo(.*?)bar/g; print "$_ \t$1\n"; $text =~ m/foo(.*?)bar/g; print "$_ \t$1\n"; #[... repeated as often as necessary ...]
Here $1 is set to the (one thing) you just matches with <kbd>(.*?)</kbd>, $_ is not set at all.
-- Brigitte 'I never met a chocolate I didnt like' Jellinek http://www.horus.com/~bjelli/ http://perlwelt.horus.at
|
|---|