in reply to Re: Difference between tr// and s///?
in thread Difference between tr/// and s///?

Notice there's a space between the second and third slash in the OP's code example. With proper code tags you can see more clearly:

$toto = 'this+is+my+text'; $toto =~tr/+/ /; why not $toto =~s/\+/ /g; ?

Replies are listed 'Best First'.
Re: Difference between tr// and s///?
by Abigail-II (Bishop) on Feb 06, 2004 at 11:36 UTC
    Ah, yes, I didn't realize that originally the OP didn't use <code> tags, so I didn't notice the space.

    My second point still stands though, tr/// is usually faster than s///. It is faster because tr/// is a much simpler operation. It's a character by character replacement - no copy of the string needs to be made, nor do large parts of the string be moved. An s/// operation cannot assume such a thing, replacement parts can be shorter or smaller than the thing that needs to be replaced.

    Abigail