Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Difference between tr// and s///?

by allolex (Curate)
on Feb 06, 2004 at 09:44 UTC ( [id://327027]=note: print w/replies, xml ) Need Help??


in reply to Difference between tr/// and s///?

From perlop, which you can access via the command perldoc perlop from the command line. (Try perldoc perldoc first)

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.) A character range may be specified with a hyphen, so "tr/A-J/0-9/" does the same replacement as "tr/ACEG-IBDFHJ/0246813579/". For sed devotees, "y" is provided as a synonym for "tr". If the SEARCHLIST is delimited by bracketing quotes, the REPLACEMENTLIST has its own pair of quotes, which may or may not be bracketing quotes, e.g., "tr[A-Z][a-z]" or "tr(+\-*/)/ABCD/". Note that "tr" does not do regular expression character classes such as "\d" or "[:lower:]". The <tr> operator is not equivalent to the tr(1) utility. If you want to map strings between lower/upper cases, see "lc" in perlfunc and "uc" in perlfunc, and in general consider using the "s" operator if you need regular expressions.
s/PATTERN/REPLACEMENT/egimosx
Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made. Otherwise it returns false (specifically, the empty string). If no string is specified via the "=~" or "!~" operator, the $_ variable is searched and modified. (The string specified with "=~" must be scalar variable, an array element, a hash element, or an assignment to one of those, i.e., an lvalue.) If the delimiter chosen is a single quote, no interpolation is done on either the PATTERN or the REPLACEMENT. Otherwise, if the PATTERN contains a $ that looks like a variable rather than an end-of-string test, the variable will be interpolated into the pattern at run-time. If you want the pattern compiled only once the first time the variable is interpolated, use the "/o" option. If the pattern evaluates to the empty string, the last successfully executed regular expression is used instead. See perlre for further explanation on these. See perllocale for discussion of additional considerations that apply when "use locale" is in effect

--
Allolex

Replies are listed 'Best First'.
Re: Difference between tr// and s///?
by Abigail-II (Bishop) on Feb 06, 2004 at 11:30 UTC
    Uhm, how does that explain why tr/+/ / is used instead of s/\+/ /g? The post of the OP clearly suggest to me that the OP knows what tr/// and s/// do.

    Abigail

      It was this sentence:

      kalamiti: I know both works but get lost on wich to choose

      I felt that kalamiti was not really sure about what s/// and tr/// do, since they both were not doing the same thing in the code, and decided that adding some more documentation to your post (which was already the highest-voted node at the time) would be a good idea.

      Abigail-II: The post of the OP clearly suggest to me that the OP knows what tr/// and s/// do.

      I didn't get the same impression.

      --
      Allolex

      It's a frightening trend. Whenever someone asks a question, you'll usually get someone replying by copying & pasting perldoc info. In my view, they are trying to look smart by doing so and perhaps get some XP. (Hey, look at me, I know how to use perldoc!)

        While I agree in principle, that's still a harsh critisism.

        Instead, perhaps a comprimise, suggesting the person look in x or y perldoc, and search for z entry? Or a link to the same on http://perldoc.com rather than repasting stuff we all have access to anyway? If someone doesn't know how to look, they should certainly learn, but simply reposting from perldoc isn't going to help.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 05:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found