Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





Regular Expression Operator tr/// in Perl


Tutorials Perl

Topic

What is Regular Expression Operator tr/// in Perl?



Explanation

The Regular Expression tr/// operator is used to change a set of characters into another set of characters as its not possible with "s//" operator, in this the second argument has replacement characters and only at compile time it checks the strings to be replaced.One can append "d", "c", "s" to add different functionality to the "tr" operator.

If the "d" option is used it delete's found but unreplaced characters, if "c" is appended complements or inverts the searchlist,"s" option is appended to squash duplicate replaced characters.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $string = 'maurice mike is the father of morris mike';
    $string2 = 'maurice mike';
    $string =~ tr/abcde/ABCDE/;
    print $string;
    print "<br>";
    $string2 =~ tr/a-z/x/;
    print $string2;
Result:
    mAuriCE mikE is thE fAthEr of morris mikE

In the above tr operator example first the lower case "abcde" is replaced to uppercase "ABCDE" where ever it is found.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $string2 = 'maurice mike';
    $string2 =~ tr/a-z/x/;
    print $string2;
Result:
    xxxxxxx xxxx

In the above tr/// operator example length of the replacement character "x" is 1. So "x" is repeated to the length of the string "string2".

Appending "d" to "tr" operator:

To remove the characters which are not in the matching list can be done by appending "d" to the end of the "tr" operator.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $string = 'my name is sam anderson';
    $string =~ tr/a-z/ABCDE/;
    print $string;
Result:
    EE EAEE EE EAE AEDEEEEE

In the above tr operator example all the characters are replaced with the uppercase "ABCDE", if the no matching characters are found by default it replaced by "E".

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $string2 = 'my name is sam anderson';
    $string2 =~ tr/a-z/ABCDE/d;
    print $string2;
Result:
    AE A ADE

In the tr/// operator example when appended with "d" not macthing characters in the string is removed, and only the matching character replaced with its uppercase is displayed.

Appending "c" to "tr" operator:

To complement the string with respect to all 256 characters, any character specified in the string is removed then the remaining characters are taken to match the pattern.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    $string = 'my name is sam anderson';
    $string =~ tr/a-z/_/c;
    print $string;
Result:
    my_name_is_sam_anderson

In the above tr operator example the "tr" operator leaves "a-z" in the string as it uses "c" operator, only other character left in the string is the blank space, so the replacement character "_" is replaced to all the blank spaces.

Appending "s" to "tr" operator:

To replace duplicate characters with the charater specified is done by appending the "s" to the end of the "tr" operator.

Example:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    my $text = 'good freeze';
    $text =~ tr/eo/eu/s;
    print "$text\n";
Result:
    gud freze

In the above tr/// operator example the letters "o" and "e" is repeated twice, so by appending "s", "o" is replaced with "u" once and "e" with "e" again once and displays the result.






A Note
Simple introduction, basic CGI perl programming codes with examples. Do send your feedback or suggestions on this tutorial. This is a copyright content.

Other Links

web hosting