Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Your global substitution matches the string you gave it three times.

The first match only matches "this" and stores it in $1. $2 either contains an empty string or is undefined (I don't know which as it's never mattered to my code).

The second match doesn't match "this" or "that" so it moves on to one or more word characters and matches "and" and stores it in $2. This time $1 is undef.

The last match matches "that" and stores it once again in $1. $2 is undef.

It seems as if you are possibly confused by either the number of times the substitution actually matches or by which portion of the string gets assigned to $1 and $2.

The reason why you are getting the warning, is that each time the substitution matches, the replacement pattern contains an uninitialized value in $2, $1, and $2 respectively.

If you are having trouble figuring out which portion of the string would be assigned to $1 and $2, then the following code may help. If you can count left parentheses, you can figure out what part of the string will be assigned to which variable from left to right. For instance the following string will match as follows.
$string = "A Perl among Camels"; $string =~ /(((\w+) (\w+)) ((\w+) (\w+)))/g; print "1 is '$1'\n 2 is '$2'\n 3 is '$3'\n 4 is '$4'\n 5 is '$5'\n 6 is '$6'\n 7 is '$7'\n";

returns:
1 is 'A Perl among Camels' 2 is 'A Perl' 3 is 'A' 4 is 'Perl' 5 is 'among Camels' 6 is 'among' 7 is 'Camels'

In reply to Re: why such an error happened? by krusty
in thread why such an error happened? by lightoverhead

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (7)
    As of 2014-04-19 00:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (473 votes), past polls