- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The Translation Operator in Perl
Translation is similar, but not identical, to the principles of substitution in Perl, but unlike substitution, translation (or transliteration) does not use regular expressions for its search on replacement values. The translation operators are −
tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cds
The translation replaces all occurrences of the characters in SEARCHLIST with the corresponding characters in REPLACEMENTLIST. For example, using the "The cat sat on the mat." string we have been using in this chapter −
Example
#/user/bin/perl $string = 'The cat sat on the mat'; $string =~ tr/a/o/; print "$string\n";
When above program is executed, it produces the following result −
The cot sot on the mot.
Standard Perl ranges can also be used, allowing you to specify ranges of characters either by letter or numerical value. To change the case of the string, you might use the following syntax in place of the uc function.
$string =~ tr/a-z/A-Z/;
Translation Operator Modifiers
Following is the list of operators related to translation.
Sr.No | Modifier & Description |
---|---|
1 | c Complements SEARCHLIST. |
2 | d Deletes found but unreplaced characters. |
3 | s Squashes duplicate replaced characters. |
The /d modifier deletes the characters matching SEARCHLIST that do not have a corresponding entry in REPLACEMENTLIST. For example −
Example
#!/usr/bin/perl $string = 'the cat sat on the mat.'; $string =~ tr/a-z/b/d; print "$string\n";
When above program is executed, it produces the following result −
b b b.
The last modifier, /s, removes the duplicate sequences of characters that were replaced, so −
Example
#!/usr/bin/perl $string = 'food'; $string = 'food'; $string =~ tr/a-z/a-z/s; print "$string\n";
When above program is executed, it produces the following result −
fod
- Related Articles
- The ? : Operator in Perl
- The Match Operator in Perl
- The Substitution Operator in Perl
- Backstick Operator in Perl
- What is the Syntax Directed Translation?\n
- What is a "translation unit" in C++
- Translation and Post-Translational Modifications
- Translation Mechanism and Clinical Significance
- What are the functions of network address translation?
- Adding translation to a model instance in Django
- The $[ Special Variable in Perl
- The Infinite Loop in Perl
- The carp Function in Perl
- The cluck Function in Perl
- The croak Function in Perl
