## \$PIECE

### M[UMPS] by Example

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

This function returns a substring of the value passed as a parameter.

Set X="ABC*DEF"

Reference Value \$Piece(X,"*",1) "ABC" \$Piece(X,"*",2) "DEF" \$Piece(X,"*",3) "" (empty string) \$Piece(X,"B",1) "A"

Set Y="B"

Reference Value \$Piece(X,Y,1) "A" \$Piece(X,Y,2) "C*DEF" \$Piece(X,"/",1) "ABC*DEF"

Set X="A.B.C.D"

Reference Value \$Piece(X,".",1) "A" \$Piece(X,".",2,3) "B.C" \$Piece(X,".",1,100) "A.B.C.D" \$Piece(X,".",–5,2) "A.B" \$Piece(X,".",3,2) "" \$Piece(X,"",1,100) ""

Addition in the 1984 ANSI M[UMPS] language standard.

It is allowed to specify a reference to \$Piece on the left hand side of the equal sign in a Set command.

Set ^DATA("SUB")="11^22^33^44^55"

Reference Value Set \$Piece(^DATA("SUB"),"^",3)=123 "11^22^123^44^55" Set \$Piece(^DATA("SUB"),"^",3,4)="three^four" "11^22^three^four^55" Set \$Piece(^DATA("SUB"),"^",3)="a^b^c" "11^22^a^b^c^four^55" Set \$Piece(^DATA("SUB"),"^",2,4)="" "11^^c^four^55"

Set X="This is an test"

Reference Value Set \$Piece(x," ",3)="a" X="This is a test" Set \$Piece(x," ",2,3)="was no" X="This was no test" Set \$Piece(x," ",3)="a" X="This was a test"

Kill ^X
Set ^X(1,2,3)=9
Set ^X(1,2)=7
Set \$Piece(^(2),";",3,^X(1,2,3))=6
Write ^(2)

Let's trace the order of evaluation, and see what happens to the naked indicator:
Set ^X(1,2,3)=9
naked indicator is ^X(1,2,
Set ^X(1,2)=7

naked indicator is ^X(1,

fetch ^X(1,2,3); naked indicator is ^X(1,2,
fetch ^(2) = ^X(1,2,2), which is 'undefined' (error: M7)

So, let's resolve that problem:
Kill ^X
Set ^X(1,2,3)=9
Set ^X(1,2,2)=8
Set ^X(1,2)=7
Set \$Piece(^(2),";",3,^X(1,2,3))=6
Write ^(2)

Now that all referenced variables are defined, the command is evaluated
as:
Set \$Piece(^X(1,2,2),";",3,9)=6
so that the value of ^X(1,2,2) becomes "8;;6"

Set ^X("Y")=1
Set \$Piece(^X("Y"),";",2,1)=0
Write ^X("Y")

will produce the value 1. Because in the Set \$Piece, the until value is less than the from value, no information is changed, and the naked indicator is unaffected.

Examples with naked references:

\$Piece(VALUE,STRING)
Set ^ABC(1,2)="reset naked indicator"
; naked indicator is now ^ABC(1,
Set ^(3,4)=\$Piece(^(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,

\$Piece(VALUE,STRING,PIECE)
Set ^ABC(1,2)="reset naked indicator"
; naked indicator is now ^ABC(1,
Set ^(3,4)=\$Piece(^(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,

\$Piece(VALUE,STRING,FROM,TO)
Set ^ABC(1,2)="reset naked indicator"
; naked indicator is now ^ABC(1,
Set ^(3,4)=\$Piece(^(5,6),^(7,8),^(9,10),^(11,12))

; 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. fetch ^(11,12) = ^ABC(1,5,7,9,11,12)
; 5. store ^(3,4) = ^ABC(1,5,7,9,11,3,4)
; naked indicator is now: ^ABC(1,5,7,9,11,3,

Set \$Piece(VALUE,STRING,FROM,TO)=VALUE
Set ^ABC(1,2)="reset naked indicator"
; naked indicator is now ^ABC(1,
Set \$Piece(^(3,4),^(5,6),^(7,8),^(9,10))=^(11,12)

; 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. fetch ^(11,12) = ^ABC(1,5,7,9,11,12)
; 5. if ^ABC(1,5,7,9,10) < ^ABC(1,5,7,8) or
; if ^ABC(1,5,7,9,10) < 1
; don't store, naked indicator is ^ABC(1,5,7,9,11,
; else continue
; 6. fetch/store ^(3,4) = ^ABC(1,5,7,9,11,3,4)
; naked indicator is now: ^ABC(1,5,7,9,11,3,