Basic HTML version of Foils prepared March 30 97

Foil 55 The tr translation operator (Chapter 15 of Llama Book)

From PERL4 Tutorial for CPS616 Computational Science for Information Age Course CPS616 -- February 1995. by Geoffrey C. Fox


1 tr/ab/XY/ translates a to X and b to Y in string $_
2 As for m and s, one can apply tr to a general string with =~
3 $string =~ tr/a-z/A-Z/; # translates letters from lower to upper case in $string
4 Note use of - to specify range as in regular expressions although tr does NOT use regular expressions
5 tr can count and returns number of characters matched
6 $numatoz = tr/a-z//; # $numatoz holds number of lower case letters in $_
7 if final string empty no substitutions are made
8 if second string shorter than first, the last character in second string is repeated
9 tr/a-z/A?/; # replaces a by A and all other lower case letters by ?
10 if the d option used, unspecified translated characters are deleted
11 tr/a-z//d; # deletes all lower case letters
12 the c option complements characters in initial string
13 tr/a-zA-Z/_/c; # translates ALL nonletters into _
14 the s option squeezes multiple consecutive copies of any letter in final string and replaces them by a single copy.

in Table To:


© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sat Sep 6 1997