Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Question about use of tr/ / / and s/ / /g

by Jemts (Monk)
on May 06, 2001 at 19:04 UTC ( #78351=perlquestion: print w/replies, xml ) Need Help??

Jemts has asked for the wisdom of the Perl Monks concerning the following question:

Is there any practical difference between tr/pattern/replacment/ and s/pattern/replacment/g

Jemts

"If you're traveling in a time machine, and you're eating corn on the cob, I don't think it's going to affect things one way or the other. But here's the point I'm trying to make: Corn on the cob is good, isn't it."
  • Comment on Question about use of tr/ / / and s/ / /g

Replies are listed 'Best First'.
(jeffa) Re: Question about use of tr/ / / and s/ / /g
by jeffa (Bishop) on May 06, 2001 at 19:18 UTC
    Yes. The terse explanation can be perused at perlop, but here is simple explanation in the meantime:

    tr/// is the transliteration operator. It is used to convert all occurances of one character class to another:

    tr/0-9/A-J/
    s/// is the substitution operator. It replaces a pattern with some replacement string:
    s/foo/bar/
    The differences in the results can be subtle. A great explanation of when to use to which (don't rule out substr as a solution either) can be found at Code Smarter.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Question about use of tr/ / / and s/ / /g
by merlyn (Sage) on May 06, 2001 at 19:33 UTC
    If it's a single character to other single character, or single character to empty mapping, without regard to surrounding character context, tr/// will certainly beat s///, as it's the simpler operator to do so.

    So, use tr/// if it can do the job, otherwise use s///.

    -- Randal L. Schwartz, Perl hacker

Re: Question about use of tr/ / / and s/ / /g
by virtualsue (Vicar) on May 06, 2001 at 19:19 UTC
    Yes indeed - mainly because tr/// doesn't understand regular expressions.
Re: Question about use of tr/ / / and s/ / /g
by mincus (Chaplain) on May 06, 2001 at 19:20 UTC
    tr/// does transliteration... it maps characters to other characters.

    s/// will take a pattern and replace the pattern...

    This will explain it in much more detail...

    Best of luck!

    UPDATE:
    Listen to jeffa, he makes much more sence than I... but the best way to understand the difference is still Here


    .mincus
    telnet://bbs.mincus.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://78351]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-10-18 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (74 votes). Check out past polls.

    Notices?