Returns a string that results from replacing or dropping characters in the first of its arguments as specified by the patterns of its other arguments.

The format for the $TRANSLATE function is:

$TR[ANSLATE](expr1[,expr2[,expr3]])

The $TRANSLATE() algorithm can be understood as follows:

  • $TRANSLATE() evaluates each character in the first expression, comparing it character by character to the second expression looking for a match. If there is no match in the second expression, the resulting expression contains the character without modification.

  • When it locates a character match, $TRANSLATE() uses the position of the match in the second expression to identify the appropriate replacement for the original expression. If the second expression has more characters than the third expression, $TRANSLATE() replaces the original character with a null, thereby deleting it from the result. By extension of this principle, if the third expression is missing, $TRANSLATE() deletes all characters from the first expression that occur in the second expression.

Example:

GTM>write $translate("ABC","CB","1")
A1
GTM>
[Note]Note

While this example provides an explanation for the work done by $TRANSLATE(), it does not necessarily correspond to how GT.M implements $TRANSLATE().

Example:

GTM>write $translate("A","AA","BC")
B
GTM>

This $TRANSLATE() example finds the first occurrence of "A" in the second expression, which holds the first character position, and substitutes the character in the first position of the third expression.

Example:

GTM>write $translate("BACKUP","AEIOU")
BCKP
GTM>

Because the $TRANSLATE() has only two parameters in this example, it finds the characters in the first expression that also exist in the second expression and deletes them from the result.

loading table of contents...