Set

SET assigns values to variables or to a selected portion of a variable.

The format of the SET command is:

S[ET][:tvexpr] setleft=expr | (setleft[,...])=expr | *lvn=lname | aliascontainer[,...]

where

setleft == glvn | $EXTRACT(glvn,[,intexpr1[,intexpr2]]) | $PIECE(glvn,expr1[,intexpr1[,intexpr2]]) | isv

and

aliascontainer == lvn | exfunc | exvar

For the syntax of $PIECE() or $EXTRACT(), refer to Chapter 7: “Functions.

Examples of SET

Example:

GTM>Kill  Set a="x",(b,c)=1,@a="hello" ZWRite
a=x
b=1
c=1
x="hello"
GTM>

The KILL command deletes any previously defined local variables. The SET command has three arguments. The first shows a simple direct assignment. The second shows the form that assigns the same value to multiple variables. The third shows atomic indirection on the left of the equal sign. The ZWRITE command displays the results of the assignments.

Example:

GTM>Set ^(3,4)=^X(1,2)

As GT.M evaluates the right-hand side of the equal sign before the left-hand side within a SET argument, the right-hand expression determines the naked reference indicator prior to evaluation of the left-hand side. Therefore, this example assigns ^X(1,3,4) the value of ^X(1,2).

Example:

GTM>Kill x Set $Piece(x,"^",2)="piece 3" ZWRite x
x="^^piece 3"
GTM>

This SET demonstrates a "set piece" and shows how SET generates missing delimiters when required. For more information on $PIECE(), refer to Chapter 7: “Functions.

Example:

GTM>Set x="I love hotdogs"
                
GTM>Set $Extract(x,3,6)="want"
GTM>Write x
I want hotdogs
GTM>Set $Extract(x,7)=" many "
GTM>Write x
I want many hotdogs
GTM>

The SET $EXTRACT command replaces and extracts the specified characters with the value of the expression on the right hand side of the equal-sign (=). For more information on $EXTRACT(), refer to Chapter 7: “Functions.

Example:

GTM>kill A,B
        
GTM>set A=1,A(1)=1,A(2)=2
GTM>set *B=A ; A & B are aliases. 
GTM>zwrite B
B=1 ;*
B(1)=1
B(2)=2
GTM>

This SET * command creates an alias associated between A and B. It associates the entire tree of nodes of A including its root and all descendants with B.

Example:

GTM>kill A,B,C
          
GTM>set A=1,*C(2)=A ; C(2) is a container
GTM>zwrite
A=1 ;*
*C(2)=A
GTM>set *B=C(2) ; B is now an alias
GTM>write B,":",$length(C(2)),":" ; An alias variable provides access but a container doesn't
1:0:
GTM>

This SET * command creates an alias by dereferencing an alias container.