in reply to howto replace part of a string

So, using substr as an lvalue worked (so my problem is solved :)
But I still don't understand why the following example doesn't work:
$a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ; ($a = $a ) =~ s/^\S{5}(\S{4})\S+/\1xxxx/ ;
any suggestions ?

LuCa

Replies are listed 'Best First'.
Re^2: howto replace part of a string
by Tanktalus (Canon) on Jun 14, 2006 at 15:28 UTC

    Well, for starters, you're using 5, 4, and *, rather than 10 and 20. Secondly, you're ignoring the warning message:

    \1 better written as $1 at ./z.pl line 7.
    (You are using strict and warnings, right?) Third - what's with the ($a = $a )? Finally, you've inverted the problem. You're replacing everything except characters 6 through 9. You probably want something like:
    $a =~ s/^(\S{5})\S{4}/$1xxxx/;
    Or, potentially simpler:
    $a =~ s/(?<=^\S{5})\S{4}/xxxx/;
    But what you really want is still substr.

      So, now I understand what I was doing (or not doing). I never understood the \1, and I don't know why I do ( $a = $a ) too, I think I once copied it from somewhere without understanding it

      Thanks a lot
      Luca
Re^2: howto replace part of a string
by CountOrlok (Friar) on Jun 14, 2006 at 15:26 UTC
    Is this what you're trying to do?
    $a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $a =~ s/^\S{5}(\S{4})\S+/$1xxxx/ ;
    -imran
      You're right, but I realize now, because I didn't understand the expression, that it is not what I want!