in reply to Replace the last letter

This will replace the last character...
$find = 's'; $replace = 'X'; $string = 'Snakes'; $string =~ s/$find$/$replace/; print $string, "\n";

Replies are listed 'Best First'.
Re^2: Replace the last letter
by gothic_mallard (Pilgrim) on Oct 07, 2004 at 17:22 UTC

    Personally I'd go down the regex route too for preference. If the find/replace chars that you're using are always the same then you can improve the effeciency with just:

    $string = 'Snakes'; $string =~ s/s$/X/;

    If you want to take the substr() route then try:

    substr($string,-1,1,'X');

    That will replace the last character whatever it is with the X. For more details I'd also point you towards the perldocs - if you don't have access to the perldoc function then look it up on http://www.perldoc.com.

    --- Jay

    All code is untested unless otherwise stated.

      If you always want the last letter, you could match:

      $string =~ s/.$/X/; ## or $letter = 'X'; $string =~ s/.$/$letter/;
      As opposed to the substr use.

      radiantmatrix
      require General::Disclaimer;
Re^2: Replace the last letter
by Roger (Parson) on Oct 08, 2004 at 02:08 UTC
    What about replacing the last occurance of 's' in the word 'systematic'? I think the OP should have stated the question more clearly, that he wants the last occurance of a certain letter, not the last letter of the string.