http://www.perlmonks.org?node_id=250581

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

Hello Perl monks,
I wrote the following code
my $a = "The quick brown fox jumped over the lazy dog"; $a =~ tr/\n\n/\<br\/\>/; print "$a\n";

It produced the wrong results "The quick<<brown fox<<jumped<<over the<<lazy<<dog"

I removed the tr/ statement and inserted the following code $a =~ s/\n\n/\<br\/\>/g;

and the program gave me the right results "The quick<br/>brown fox<br/>jumped<br/>over the<br/>lazy<br/>dog"

But I can't understand why did the tr/ didn't work and why s/ did? what is the differnce between them. I used to think that they are two ways of doing the same thing.

thanks for your help.

regards,
Abhishek.

Replies are listed 'Best First'.
Re: What is the difference between s/ and tr/
by broquaint (Abbot) on Apr 15, 2003 at 15:01 UTC
    tr/// transliterates characters, and s/// substitutes, basically. In your example code, when you use tr/// you're replacing every occurance of the character \n with < in the string, whereas s/// substitutes every occurrence of the substring \n\n with <br/>. See. perlop for more info on tr/// and s///.
    HTH

    _________
    broquaint

Re: What is the difference between s/ and tr/
by Heidegger (Hermit) on Apr 15, 2003 at 15:03 UTC

    Transliterate: transliterates all occurrences of the characters found in the search list with the corresponding character in the replacement list. It returns the number of characters replaced or deleted.

    Substitute replaces the first regexp with the second.

    In your example transliterate changes the new line character (\n) with the character '<'. In your example substitute changes the first regexp (2 new lines) with a HTML break.

Re: What is the difference between s/ and tr/
by valdez (Monsignor) on Apr 15, 2003 at 15:06 UTC

    From perlop:

    tr/SEARCHLIST/REPLACEMENTLIST/cds
    y/SEARCHLIST/REPLACEMENTLIST/cds

    Transliterates all occurrences of the characters found in the search list with the corresponding character in the replacement list. It returns the number of characters replaced or deleted. If no string is specified via the =~ or !~ operator, the $_ string is transliterated. (The string specified with =~ must be a scalar variable, an array element, a hash element, or an assignment to one of those, i.e., an lvalue.)

    From perlrequick:

    Search and replace
    Search and replace is performed using "s/regex/replacement/modifiers". The "replacement" is a Perl double quoted string that replaces in the string whatever is matched with the "regex". The operator "=~" is also used here to associate a string with "s///". If matching against $_, the "$_ =~" can be dropped. If there is a match, "s///" returns the number of substitutions made, otherwise it returns false.

    Ciao, Valerio

Re: What is the difference between s/ and tr/
by Mr. Muskrat (Canon) on Apr 15, 2003 at 16:24 UTC
    Everyone who posted before me is correct. However, I thought that someone should point out that tr does not understand regular expressions or regex character classes. The similarity to regexes ends at the binding characters (=~ or !~) and the ability to use the same separation characters.
Re: What is the difference between s/ and tr/
by mce (Curate) on Apr 15, 2003 at 15:05 UTC
    The difference is that the s does a regular expression/substitution and the tr a transliteration.
    tr replaces each occurrence of a character by the other, so you actually wrote:
    tr/\n/</
    so, replace each new line with a <. Start by reading perldoc perlop, and learn it by heart :-)
    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium
Re: What is the difference between s/ and tr/
by dze27 (Pilgrim) on Apr 15, 2003 at 16:00 UTC
    I think you might have been originally confused because in certain very specific cases, tr/ and s/ do the same thing. For example, s/a/b/g is the same as tr/a/b/ -- they both replace all instances of a with b. Anything more complex than this and it doesn't work though. You need to think of these as completely separate functions.