in reply to Re: $1 doesn't reset?
in thread $1 doesn't reset?
OK, so, from perlman :$_='axxbcaxbagaxbacba'; foreach my $a (m/a(.*?)b/g) { print "$a\n"; }
So, right away, we see that foreach doesn't care about, or understand $1 and company at all. It only knows about its control variable ($a, in this case) and its list : (m/a(.*?)b/g).
The foreach modifier is an iterator: For each value in EXPR, it aliases $_ to the value and executes the statement
So, with this regexp, you get an anonymous list of 4 elements. It's like writing
(m//) ... in a list context returns a list consisting of the subexpressions matched by the parentheses in the pattern, i.e., ($1, $2, $3...)
by the time the code in the loop's running, the regexp has run and returned a list of values that foreach will process.foreach my $a ($1, $2, $3, $4) {
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (boo) Using regexps as lists in loops
by baku (Scribe) on Mar 20, 2001 at 23:54 UTC |