Search and Replace

Discussion in 'Perl Misc' started by vspriya05@gmail.com, Dec 6, 2005.

  1. Guest

    Hi,

    I want to replace a string with perl variable which has the
    text \${IMAGEPATH}. I gave as follows. but it is not working. Can
    anybody help me?

    The code is

    $replace='\${IMAGEPATH}';
    $string="http://image.com";

    $string=~ s/$find/$replace/g;
    , Dec 6, 2005
    #1
    1. Advertising

  2. wrote in news:1133869308.010095.175370
    @f14g2000cwb.googlegroups.com:

    > I want to replace a string with perl variable which has the
    > text \${IMAGEPATH}. I gave as follows. but it is not working. Can


    It looks like you are trying to implement a templating solution. There
    is no benefit to doing this from scratch: Just use one of the many
    excellent templating modules, such as Template-Toolkit, HTML::Template,
    Text::Template etc on CPAN.

    > The code is


    Please do post the code you are using (reduced to a short but complete
    script).

    > $replace='\${IMAGEPATH}';
    > $string="http://image.com";
    >
    > $string=~ s/$find/$replace/g;


    Where did $find come from? What are you specifying the g modifier?

    I would have thought you would want to replace \${IMAGEPATH} with
    http://image.com. What are you really trying to do?

    Please read and follow the posting guidelines for this group.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Dec 6, 2005
    #2
    1. Advertising

  3. wrote in news:1133869308.010095.175370
    @f14g2000cwb.googlegroups.com:

    > Hi,
    >
    > I want to replace a string with perl variable which has the
    > text \${IMAGEPATH}. I gave as follows. but it is not working. Can
    > anybody help me?
    >
    > The code is
    >
    > $replace='\${IMAGEPATH}';
    > $string="http://image.com";
    >
    > $string=~ s/$find/$replace/g;


    Characters like $ and { are special within regexes. If you want to replace
    the literal string, you need to quote-escape it:

    $string =~ s/\Q$find/$replace/g;

    --
    Eric
    `$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
    $!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
    $_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
    ;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
    Eric J. Roode, Dec 6, 2005
    #3
  4. Guest

    hi,

    I want to replace "http://image.com" with \${IMAGEPATH}.

    Thanks,
    Priya
    , Dec 6, 2005
    #4
  5. Brian Wakem Guest

    wrote:

    > Hi,
    >
    > I want to replace a string with perl variable which has the
    > text \${IMAGEPATH}. I gave as follows. but it is not working. Can



    Not working? A better explanation will be needed.


    >
    > $replace='\${IMAGEPATH}';
    > $string="http://image.com";
    >
    > $string=~ s/$find/$replace/g;



    $find in undefined.


    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
    Brian Wakem, Dec 6, 2005
    #5
  6. wrote:

    > I want to replace "http://image.com" with \${IMAGEPATH}.


    What to you mean by \${IMAGEPATH} ?

    Do you mean the literal sequence backslash dolar left-brace capital-I
    etc... ?

    Please try to speak Perl rather than English wherever possible. (This
    and much other valuable advice can be found in the posting gidelines).

    s'http://image.com'\${IMAGEPATH}';

    Or if you prefer "leaning toothpick symdrome"

    s/http:\/\/image.com/\\\\${IMAGEPATH}/;
    Brian McCauley, Dec 6, 2005
    #6
  7. Brian McCauley wrote:

    > Or if you prefer "leaning toothpick symdrome"
    >
    > s/http:\/\/image.com/\\\\${IMAGEPATH}/;


    Opps - one toothpick too many:

    s/http:\/\/image.com/\\\${IMAGEPATH}/;
    Brian McCauley, Dec 6, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark McKay
    Replies:
    3
    Views:
    1,301
    Thomas Weidenfeller
    Jan 21, 2004
  2. Brian Blais
    Replies:
    1
    Views:
    366
    Bruno Desthuilliers
    Jun 27, 2006
  3. Greg Ewing
    Replies:
    2
    Views:
    334
    Dieter Maurer
    Jun 29, 2006
  4. Abby Lee
    Replies:
    5
    Views:
    378
    Abby Lee
    Aug 2, 2004
  5. Replies:
    1
    Views:
    511
    Rainer Weikusat
    Jun 21, 2012
Loading...

Share This Page