in reply to add text to a string

Another idea.
$string = "bcde"; $string =~ s/($_)/a$1/;

Replies are listed 'Best First'.
Re^2: add text to a string
by chargrill (Parson) on May 31, 2006 at 02:11 UTC

    Well, that won't work, but what you're going after would:

    $string = "bcde"; $string =~ s/^(.+)$/a$1/;

    Update: AM's suggestion does work (my bad), as does mine, and so would betterworld's below. However it just seems a silly way to go about it, with either $string = "a$string"; or $string = 'a' . $string; being the more traditional ways of doing it.



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}
      Wouldn't it be more straightforward to do it like this:
      $string =~ s/^/a/;
Re^2: add text to a string
by jeanluca (Deacon) on May 31, 2006 at 08:08 UTC
    I think I red somewhere that using the variables like $1 which you get from that regular exopression makes you program slower!

    Luca