Let me try to make it clear using another example...

Take the regexp /(\w)(\w)/, and the string "same old show".

$_ = "same old show"; # scalar, one at a time: while(/(\w)(\w)/g) { print "while: $1 (+ $2)\n"; } # list context, one go @list = /(\w)(\w)/g; print "List: @list\n"; # list context, foreach loop foreach(/(\w)(\w)/g) { print "foreach: $_\n"; }
Result:
while: s (+ a)
while: m (+ e)
while: o (+ l)
while: s (+ h)
while: o (+ w)
List: s a m e o l s h o w
foreach: s
foreach: a
foreach: m
foreach: e
foreach: o
foreach: l
foreach: s
foreach: h
foreach: o
foreach: w
As you can see, the foreach produces twice as much output as the while. That is because each match returns a list of two items, $1 and $2, and the regexp in list context returns a (flattened) list of such lists, as you can see in the example in the middle. foreach uses this grand, flattened list to loop through, thus for each each match you see a loop for $1, and next one for $2.

OTOH the while takes one pair of matched items each time, thus it'll loop half as many times.

In your particular example, the regexp is /((.))/. You still have a $1 and a $2, even though they capture the same thing — but you ignore $2. That's why you don't see it appear. Modifying my code to match your regexp and string, I get:

$_ = "abc"; # scalar, one at a time: while(/((.))/g) { print "while: $1 (+ $2)\n"; } # list context, one go @list = /((.))/g; print "List: @list\n"; # list context, foreach loop foreach(/((.))/g) { print "foreach: $_\n"; }
Result:
while: a (+ a)
while: b (+ b)
while: c (+ c)
List: a a b b c c
foreach: a
foreach: a
foreach: b
foreach: b
foreach: c
foreach: c
which matches your result.

In summary: you simply ignored to output the contents of $2 in your first snippet, the while loop.


In reply to Re: Double Capturing Parentheses by bart
in thread Double Capturing Parentheses by pbeckingham

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.