MUMPS Examples/Determine if current user is a valid user
This code is commonly used in an KIDS/Environment Check Routine to determine if the person installing the KIDS build is actually a user on the system. Typically, if a person's DUZ does NOT refer to a user on the system, a program cannot send e-mail to that user, add them to mail groups, or even meaningfully create an audit Record for activities they are doing as part of the install of the KIDS build.
I $D(DUZ)#2 N DIC,X,Y S DIC=200,DIC(0)="N",X="`"_DUZ D ^DIC I Y>0 E W !,"You must be a valid user."
Expanded out, this code is:
+1 IF $DATA(DUZ)#2 NEW DIC,X,Y SET DIC=200 SET DIC(0)="N" SET X="`"_DUZ DO ^DIC IF Y>0 +2 IF '$TEST WRITE !,"You must be a valid user."
Notice that the code uses both the IF and the ELSE commands, that it depends on the line-scoping of IF and ELSE, and that the first (IF) line ends with an IF command to specifically modify the $TEST variable so the ELSE command will function properly.
The ELSE line, in effect, is attached to two separate IF commands, and is activated if either of their truthvalues is false.
1) The first IF-argument tests whether there is an actual value for the variable DUZ, explicitly ignoring any "subscript" values.
2) The second IF-argument tests whether the lookup program ^DIC has signalled a failure condition by returning a value less than or equal to zero. (usually this is a -1 to tell that the call has failed)
The lookup on the first line is on the NEW PERSON File #200, and while the DIC(0) states it is allowing the lookup of a "numeric" value in the variable X, it is actually using the character corresponding to $C(96) (called GRAVE ACCENT) with the number of the value of the variable DUZ as the lookup value. This is a method of looking up a particular entry in a file if you know its Internal Entry Number. (equivalent to looking up a row in an SQL Table using its rowid).