using a regex resets the $n($1,$2,etc.) variables all to undefined regardless...

As it happens, this is not always the case. It is true that if the second regex succeeds, unused numbered variables from a previous regex do become undefined -- even if the new regex contains no capturing paren's.

But in the case given in the question, the $1 will not be undefined until after the second regex is evaluated. So the $1 in the second regex will reflect the value from the first regex if it succeeded. Example:

my $str = "a2b c34d"; $str =~ /(\D\d\D)/; print "$1\n"; # prints: 'a2b' $str =~ s/$1/NEW/; print "$str\n"; # prints: 'NEW c34d'

As different hazzard, (distinct from the example in the stated question) if the second regex fails, the previous values are retained (not undefined). This can cause intermittent problems that can be hard to trace. Consider:

my $str = "a2b c34d"; $str =~ /(\D\d\D)/; print "$1\n"; # prints 'a2b' $str =~ /(xyz)/; print "$1\n"; # prints 'a2b' again

In reply to Re: Re: Read In A File, Manipulate It, Spit It Back Out by dvergin
in thread Read In A File, Manipulate It, Spit It Back Out by JojoLinkyBob

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.