$TRanslate()

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:

Examples of $TRANSLATE()

Example:

GTM>write $translate("ABC","CB","1")
A1
GTM>
  • First, $TRANSLATE() searches for "A" (the first character in the first expression, "ABC") within the second expression ("CB"). Since "A" does not exist in the second expression, it appears unchanged in the result.

  • Next, $TRANSLATE() searches for "B" (the second character in the first expression) within the second expression ("CB"). Because "B" holds the second position in the second expression ("CB"), $TRANSLATE() searches for the character holding the second position in the third expression. Since there is no second character in the third expression, $TRANSLATE() replaces "B" with a null, effectively deleting it from the result.

  • Finally, $TRANSLATE() searches for "C" (the third character in the first expression) within the second expression ("CB"), finds it in the first position, and replaces it with the number 1, which is in the first position of the third expression. The translated result is "A1."

[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.