rsriram has asked for the wisdom of the Perl Monks concerning the following question:

hi, I am writing a perl script to code a text file to XML. A line which replaces a particular tag goes like,

$_ =~ s/<CT>/<section id=\"ch$acnofm\" label=\"\" xreflabel=\"\" role=\"fm\">/g;

I have stored the chapter number in $acno, the output should look like

<section id="ch01fm" label="" xreflabel="" role="fm">

Because the static text 'fm' immediately follows a variable $acno, perl considers $acnofm as a single variable and because $acnofm does not have any value assigned to it, the replacement is made as:

<section id="ch" label="" xreflabel="" role="fm">

Could anyone help me with the syntax for this replace?

Replies are listed 'Best First'.
Re: Replacing a string with a memory variable
by BrowserUk (Patriarch) on Jun 28, 2006 at 11:34 UTC

    Use {} to delimit the variable name

    $_ =~ s/<CT>/<section id=\"ch${acno}fm\" label=\"\" xreflabel=\"\" rol +e=\"fm\">/g;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Replacing a string with a memory variable
by mickeyn (Priest) on Jun 28, 2006 at 12:11 UTC
    use ${acno} instead of $acno

    Enjoy,
    Mickey

Re: Replacing a string with a memory variable
by VSarkiss (Monsignor) on Jun 28, 2006 at 14:08 UTC