dmorgo has asked for the wisdom of the Perl Monks concerning the following question:
perl -e '$a="1b2cde5";for($a=~/(\d+)/g){$t+=$1;};print "$t\n";' perl -e '$a="1b2cde5";for($a=~/(\d+)/g){$t+=$_;};print "$t\n";'
The first one prints 15. The second one prints 8.
My confusion is with the first one. I can understand it's taking the rightmost match, but in that case why does the loop iterate three times? I would have thought if the loop iterated three times, it would put different values into $1 each time. I'm trying to understand this one. Any explanations appreciated. This is perl 5.8.4.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Looping on a match, and value of $1
by diotalevi (Canon) on Jul 24, 2004 at 00:54 UTC | |
by pg (Canon) on Jul 24, 2004 at 03:17 UTC | |
|
Re: Looping on a match, and value of $1
by NetWallah (Canon) on Jul 24, 2004 at 01:06 UTC | |
|
Re: Looping on a match, and value of $1
by Fletch (Bishop) on Jul 24, 2004 at 00:58 UTC |