Perl tr/// operator



The Perl tr/// function is used to transliterate all occurrences found in a search list with the corresponding character in a replacement list.

This function is a character by character translation and if you want to perform more complex operations you can use the s/// regex substitution operator instead.

In Perl tr/// is known better as an operator rather than a function.

The syntax form of the tr/// operator is as follows:

tr/SEARCHLIST/REPLACEMENTLIST/cds

It replaces all the occurrences of the characters in SEARCHLIST with the characters in the REPLACEMENTLIST. It returns the number of characters replaced or deleted.


Click Below to See the Best

Perl How-to Snippet Collection

and Save Hours of Surfing on Internet!


The transliteration table is built at compile time, so you can’t use interpolation either in SEARCHLIST or in REPLACEMENTLIST. If you need to use variables, you can use the eval function.

The c, d, s are modifiers that add additional functionality to the Perl tr/// operator:

  • c - is used to specify that the SEARCHLIST character set is complemented
  • d - is used to delete found but not replaced characters
  • s - is used to specify that the sequences of characters that were transliterated to the same character are squashed down to a single instance of the character
The strings can be specified via the =~, !~ binding operators or $_ by default. Please note that Perl tr/// operator does not do regular expressions character classes such as \d.

One character that has a special meaning to tr/// is the dash (-) and it indicates a range of characters (for example tr/A-C/a-c/).

The slash (/) has the meaning of a delimiter, because it separates the arguments of the tr/// operator. However, you can replace it with whatever sign of punctuation you want. The syntax changes a little if you use parentheses because they include the idea of containment.

For instance, the following syntax forms of the Perl tr/// operator are equivalents and do the same thing (it replaces any lowercase character from a to f with digits from 2 to 7 and any character from g to z with the 7 digit):

$str =~ tr/a-z/2-7/;
$str =~ tr:a-z:2-7:;
$str =~ tr[a-z][2-7];
$str =~ tr(a-z)(2-7);
$str =~ tr<2-7>;

However, if you work with Unicode characters and you need to change your text in lowercase or uppercase, don’t use the tr/// operator because it not pays attention to Unicode information. In this case you can use the \U sequence in a double quote string or the equivalent lc and uc functions.

To illustrate how to use this operator, look at the following example which shows you how you can use the tr/// operator wih the =~ binding operator:

my $string = 'one two three four five six seven';
$string =~ tr/a-z/A-Z/;

print "$string\n";
# it prints: ONE TWO THREE FOUR FIVE SIX SEVEN

This code will convert the content of the $string variable to uppercase.

Exercises


Through these exercises you have the opportunity to try yourself to write some script code where you can use the Perl tr/// operator. These exercises are completely covered in my Perl "Perl Scalar and String Functions - How To Tutorial" where I show you how to play with this important function in detail.

1. Use tr/// to convert all the elements of the array @array = qw(one two three four five six seven) into uppercase.
2. Use tr/// with the complement (c) modifier to convert the following string: '123.95,44.32,27.77,221q23' into '123 95 44 32 27 77 221 23'.
3. Use tr/// with the delete (d) modifier to convert the following string: 'aa bbb c dd eee ff mmmm rr' into '11 222 3 44'.
4. Use tr/// with the squash (s) modifier to convert the following string: '123,,,95,,44,,32,,,,27....77' into '123,95,44,32,27,77'.
5. Use tr/// with more than one modifier, for example to convert the string: 'This . is an 7example 9only' into ' This-is-an-example-only '.
6. Give an example about how to use variables, either in SEARCHLIST or in REPLACEMENTLIST of the tr/// operator.
7. Use tr/// to count the number of dots within the following string: 'This pencil is .... old. You can ..... it .....'.
8. Specify a few differences between tr/// and s///.


A-N-Y-O-N-E Can Learn and Master Perl!
And That Includes YOU!


Check these how-to tutorial eBooks (PDF format):


Table of Contents:

A Perl Script
Install Perl
Running Perl
Perl Data Types
Perl Variables
Perl Operators
Perl Lists
Perl Arrays
    Array Size
    Array Length
Perl Hashes
Perl Statements
    Perl if
    Perl unless
    Perl switch
    Perl while
    Perl do-while
    Perl until
    Perl do-until
    Perl for
    Perl foreach
Built-in Perl Functions
    Functions by Category
        String Functions
        Regular Expressions and Pattern Matching
        List Functions
        Array Functions
        Hash Functions
        Miscellaneous Functions
    Functions in alphabetical order
        chomp
        chop
        chr
        crypt
        defined
        delete
        each
        exists
        grep
        hex
        index
        join
        keys
        lc
        lcfirst
        length
        map
        oct
        ord
        pack
        pop
        push
        q
        qq
        qw
        reverse
        rindex
        scalar
        shift
        sort
        splice
        split
        sprintf
        substr
        tr (more)
        uc
        ucfirst
        undef
        unpack
        unshift
        values

return from Perl tr/// function to Perl Basics



Would you like to create your own website like this one?
Hit the Alarm Clock!

Site Build It!