Hello nvlung, and welcome to the Monastery!

LanX and choroba have answered your question, but a little code may help to make things clearer:

#! perl use strict; use warnings; my $new_msg = 'I have <c> apples, <p> pineapples, and <t> pieces of fruit. Yes, I do have <c> apples!'; my $count = 7; my $pcnt = 5; my $total = $count + $pcnt; print "\nBefore applying the substitutions, \$new_msg is:\n$new_msg\n" +; $new_msg =~ s/<c>/$count/g; $new_msg =~ s/<p>/$pcnt/g; $new_msg =~ s/<t>/$total/g; print "\nAfter applying the substitutions, \$new_msg is now:\n$new_msg +\n";

Output:

18:30 >perl 820_SoPW.pl Before applying the substitutions, $new_msg is: I have <c> apples, <p> pineapples, and <t> pieces of fruit. Yes, I do have <c> apples! After applying the substitutions, $new_msg is now: I have 7 apples, 5 pineapples, and 12 pieces of fruit. Yes, I do have 7 apples! 18:30 >

Note that the /g modifier on the substitutions “specifies global pattern matching--that is, matching as many times as possible within the string.” (perlop) If you remove the /g from the first substitution, only the first occurrence of <c> will be substituted.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: declare a variable in perl by Athanasius
in thread declare a variable in perl by nvlung

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.