The first loop executed $a =~ /(\d+)/g once producing a list of "1", "2", and "5". The last iteration of the expression left $1 set to "5" so when $_ was "1", you added "5". When $_ was "2" you added "5". When $_ was "5", you added "5".
Try the first one replacing for() with while() and you'll see what you expected. Look in perlre at the 'g' flag in list versus scalar context. Also look at perlsyn for the difference between foreach and while.
In reply to Re: Looping on a match, and value of $1
by diotalevi
in thread Looping on a match, and value of $1
by dmorgo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |