Returns the value of a local or global variable if the variable has a value. If the variable has no value, the function returns a value specified by an optional second argument, and otherwise returns an empty string.
The format for the $GET function is:
$G[ET](glvn[,expr])
The subscripted or unsubscripted global or local variable name specifies the node for which $GET() returns a value.
If the global or local variable has a data value, $GET() returns the value of the variable.
If the global or local variable has no data value, $GET() returns the value of the optional expression (second argument), or an empty string if the expression is not specified.
M defines $GET(x,y) as equivalent to:
$Select($Data(x)[0:y,1:x)
and $GET(x) as equivalent to:
$GET(x,"")
$GET() provides a tool to eliminate separate initialization of variables. This technique may provide performance benefits when used to increase the density of a sparse global array by eliminating nodes that would otherwise hold absent optional information. On the other hand, some uses of one argument $GET() can mask logic problems.
GT.M has a "NOUNDEF" mode of operation, which treats all variable references as if they were arguments to a one argument $GET(). The VIEW command controls "NOUNDEF" mode.
Example:
setstatus; if '$data(^PNT(NAME,TSTR)) set STATUS="NEW TEST" else if ^PNT(NAME,TSTR)="" set STATUS="WAITING FOR RESULT" else set STATUS=^PNT(NAME,TSTR)
This example can be reduced to two lines of code by using $GET(), shown in the following example. However, by using $GET() in its one-argument form, the distinction between an undefined variable and one with a null value is lost:
set STATUS=$get(^PNT(NAME,TSTR)) if STATUS="" set STATUS="WAITING FOR RESULT"
This is solved by using the two-argument form of $GET():
set STATUS=$get(^PNT(NAME,TSTR),"NEW TEST") if STATUS="" set STATUS="WAITING FOR RESULT"