If you don't need the list, you can of course use the match itself as for's expression.

I thought so, too :(

$ perl -e'$x="1 2 3 4 5 5 5 5 5 5"; $counts[$1]++ for $x=~/(\d)/g; pri +nt "$_ $c ounts[$_]\n" foreach (0..$#counts)' 0 1 2 3 4 5 10
How's that for a wierd problem?

Even stranger, if you s/for/while/:

$ perl -e'$x="1 2 3 4 5 5 5 5 5 5"; $counts[$1]++ while $x=~/(\d)/g; p +rint "$_ $counts[$_]\n" foreach (0..$#counts)' 0 1 1 2 1 3 1 4 1 5 6
This is with 5.6.1.

Ignore me; it makes sense that $1 would be the last value with a for loop. $_ works fine.

$ perl -e'$x="1 2 3 4 5 5 5 5 5 5"; $counts[$_]++ for $x=~/(\d)/g; pri +nt "$_ $counts[$_]\n" foreach (0..$#counts)' 0 1 1 2 1 3 1 4 1 5 6
With a while, you have to use $1; that's confused me.
$ perl -e'$x="1 2 3 4 5 5 5 5 5 5"; $counts[$1]++ while $x=~/(\d)/g; p +rint "$_ $counts[$_]\n" foreach (0..$#counts)' 0 1 1 2 1 3 1 4 1 5 6

--
Mike

In reply to Watch $1 vs. $_ in for loops... by RMGir
in thread Regex: plucking numbers from a large string by Baz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.