M[UMPS] Functions - $TR[ANSLATE]

Introduced in the 1990 ANSI M[UMPS] language standard.

This function returns a translated version of the value of the input-string.

SET UP="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
SET LO="abcdefghijklmnopqrstuvwxyz"
SET X="ABCdef"

Reference   Value
$TRANSLATE(X,LO,UP)   "ABCDEF"
$TRANSLATE(X,UP,LO)   "abcdef"
$TRANSLATE(X,LO)   "ABC"
$TRANSLATE(X,UP)   "def"

Note that case conversion using $CHAR($ASCII(X)+32) and $CHAR($ASCII(X)-32) only works within the strictly bounded 128 character ASCII set. Case conversion using $TRANSLATE will always work, regardless of the character set being used.

Assume that X="12/26/94"

Reference   Value
$TRANSLATE("Yy/Mm/Dd","Mm/Dd/Yy",X)   "94/12/26"
SET X=122694
$TRANSLATE("efabcd","abcdef",X)
  "941226"
Note: in this example, start with the string "efabcd". Replace all "a"s with the first character of the value of X, all "b"s with the second character, et cetera.

SET (FORWARD,BACKWARD)=""
FOR K=0:1:255 SET FORWARD=FORWARD_$CHAR(K)
FOR K=0:1:255 SET BACKWARD=$CHAR(K)_BACKWARD
$TRANSLATE(FORWARD,BACKWARD,"ABCDE") = "EDCBA"

$TRANSLATE(string,$TRANSLATE(string,"0123456789")) will return only the digits that occur in the value of string. Note that this result is not necessarily the same as the value of +string.

SET X="abc123cde654ghi"
$TRANSLATE(X,$TRANSLATE(X,"1234567890"))
returns 123654

The "inner" $TRANSLATE returns a value that is the original value of X, with all digits removed.
The "outer" $TRANSLATE returns a value that is the original value of X, with all characters removed that occur in the return value of the inner $TRANSLATE.
Effectively, this combination of two nested $TRANSLATEs will return only those characters from the original value of X that occur in the value of the second parameter of in "inner" $TRANSLATE.

Examples with naked references:

$TRANSLATE(VALUE,OLD)
SET ^ABC(1,2)="reset naked indicator"
; Naked indicator is now ^ABC(1,
SET ^(3,4)=$TRANSLATE(^(5,6),^(7,8))

; 1. fetch ^(5,6) = ^ABC(1,5,6)
; 2. fetch ^(7,8) = ^ABC(1,5,7,8)
; 3. store ^(3,4) = ^ABC(1,5,7,3,4)
; Naked indicator is now: ^ABC(1,5,7,3,

$TRANSLATE(VALUE,OLD,NEW)
SET ^ABC(1,2)="reset naked indicator"
; Naked indicator is now ^ABC(1,
SET ^(3,4)=$TRANSLATE(^(5,6),^(7,8),^(9,10))

; 1. fetch ^(5,6) = ^ABC(1,5,6)
; 2. fetch ^(7,8) = ^ABC(1,5,7,8)
; 3. fetch ^(9,10) = ^ABC(1,5,7,9,10)
; 4. store ^(3,4) = ^ABC(1,5,7,9,3,4)
; Naked indicator is now: ^ABC(1,5,7,9,3,


This document is © Ed de Moel, 1995-2005.
It is part of a book by Ed de Moel that is published under the title "M[UMPS] by Example" (ISBN 0-918118-42-5).
Printed copies of the book are no longer available.