$Piece()

Returns a substring delimited by a specified string delimiter made up of one or more characters. In M, $PIECE() returns a logical field from a logical record.

The format for the $PIECE function is:

$P[IECE](expr1,expr2[,intexpr1[,intexpr2]])

Examples of $PIECE()

Example:

GTM>for i=0:1:3 write !,$piece("1 2"," ",i),"<"
<
1<
2<
<
GTM>

This loop displays the result of $PIECE(), specifying a space as a delimiter, a piece position "before," first and second, and "after" the string.

Example:

GTM>for i=-1:1:3 write !,$piece("1 2"," ",i,i+1),"<"
<
1<
1 2<
2<
<
GTM>

This example is similar to the previous example except that it displays two pieces on each iteration. Notice the delimiter (a space) in the middle of the output for the third iteration, which displays both pieces.

Example:

for p=1:1:$length(x,"/") write ?p-1*10,$piece(x,"/",p)

This example uses $LENGTH() and $PIECE() to display all the pieces of x in columnar format.

Example:

GTM>set $piece(x,".",25)="" write x
........................

This SETs the 25th piece of the variable x to null, with a delimiter of a period. This produces a string of 24 periods preceding the null.

Example:

GTM>set ^x=1,$piece(^a,";",3,2)=^b

This example leaves the naked indicator to pointing to the global ^b.