Basic HTML version of Foils prepared Sept 20 97

Foil 58 The Translation Operator tr

From Tutorial on PERL Computational Science for Information Age Course CPS616 -- Sept 20 97. by Geoffrey C. Fox,Nancy McCracken,Tom Scavo


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 return 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 captures 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 Sun Sep 21 1997