in reply to Re: Transliteration
in thread Transliteration

Which brings us to another interesting question... What is the main difference between tr///? and s///? And I don't mean syntatically speaking!

Why would someone use one or the other in a specific case? I tend to be more of the sort that always uses s///'s. I have a bad habit of learning one way to do it, and sticking through with what I know works. (other examples would be the use of foreach instead of while, or !($var eq $val) instead of ($var ne $val) and I know the latter is the correct form...)

Replies are listed 'Best First'.
RE: RE: Re: Transliteration
by chromatic (Archbishop) on May 03, 2000 at 06:56 UTC
    The main difference is speed. tr/// doesn't do interpolation or use the regex engine, so it is blazingly fast in comparison. It's very good for transliterating characters (if you've ever studied a dead language that didn't use the Arabic alphabet, you'll understand).

    s/// is more flexible -- it uses the regex engine and allows character classes. It can also perform nearly abitrarily complex interpolation.

    You can get by with just s///, so if you're in doubt, go for it. Just be aware that, used correctly, tr/// will run rings around it.

RE: RE: Re: Transliteration
by perlmonkey (Hermit) on May 03, 2000 at 06:47 UTC
    I also habitually use s/// where I could use tr///.

    The main difference is with s/// you can use regular expressions, while in tr/// you cannot. Also (from the camel book) "the translation table is built at compile time, neither the SEARCHLIST nor the REPLACEMENTLIST are subject to double quote interpolation." So that means:$str =~ tr/$mylist//d; Will delete all occurances of "$, m, y, l, i, s, t" in $str. Probably not what you want.

    Also since the tr/// cannot use regex's it is usually a lot faster. I did a benchmark to that here.
RE: RE: Re: Transliteration
by BBQ (Curate) on May 03, 2000 at 06:27 UTC
    damnit, wrong link again... sorry!