Hello HereandThere, welcome to the Monastery. Hmm, did you try this code that you posted?
$_="FourThreeTwoOne, Three, Four, One, Two"; $1="Three"; $second="&&&&"; s/$2/$second/g; print;
I am pretty sure it cannot work. For a start, $1 is a read-only value that can be set only by a regex.
$1="Three";
I do not think that the perl compiler accepts that, but even if it did, it should not be done. In my view, $1 is a special variable that should be kept for just one single purpose: the first capture in a regex.
s/$2/$second/g;
This makes even less sense, since $2 has not been set anywhere, it is undefined, there is just no way this is gonna work. In addition, I would suggest that if you set $_ to something, you first localize it within some lexical block:
{ local $_="FourThreeTwoOne, Three, Four, One, Two"; # ... }
Furthermore, you should probably have these pragmas at the top of your script:
use strict; use warnings;
and they would force you to rewrite the third line of your code as:
my $second="&&&&";
Finally, I don't even understand what your code is supposed to demonstrate to the OP. Well, to tell the truth, if you were not so new on this forum, I would probably down vote your post (although I almost never down vote posts for other reasons than spamming, insults, completely off-topic posts or other clear netiquette violations). I'll refrain from doing it here in consideration of the fact that you are new here.

In reply to Re^4: RegExp substitution by Laurent_R
in thread RegExp substitution by Keystone

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.