This is a simplified version of my problem:

#!/usr/bin/perl $line="One two three four five"; if($line =~ m/(one)\s(two)\s(three)\s(four)\s(five)/i){ print "1: $1\n"; print "2: $2\n"; print "3: $3\n"; print "4: $4\n"; print "5: $5\n"; }#if

produces the expected:

[me]$>perl -w regexp-test2.pl 1: One 2: two 3: three 4: four 5: five

BUT if you try to do anything to one of the returned values such as this:

#!/usr/bin/perl $line="One two three four five"; if($line =~ m/(one)\s(two)\s(three)\s(four)\s(five)/i){ $one=$1; $one =~ s/one/ONE/i; print "1: $one\n"; print "2: $2\n"; print "3: $3\n"; print "4: $4\n"; print "5: $5\n"; }#if

you loose the other returned values:

[me]$>perl -w regexp-test3.pl 1: ONE Use of uninitialized value $2 in concatenation (.) or string at regexp +-test2.pl line 12. 2: Use of uninitialized value $3 in concatenation (.) or string at regexp +-test2.pl line 13. 3: Use of uninitialized value $4 in concatenation (.) or string at regexp +-test2.pl line 14. 4: Use of uninitialized value $5 in concatenation (.) or string at regexp +-test2.pl line 15. 5:

Can any one explain to me why this happens?

As far as my program is concerned waiting until all the returned values are assigned and then processing things works but I do not understand why doing something to a variable that has been assigned a value from $1 results in loosing the other returned values.


In reply to Regexp and substitution question by bdalzell

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.