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


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



© 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