M[UMPS] Commands

F[OR]

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

FOR I=1:1 WRITE I QUIT:I>2  WRITE "*"
To be printed: 1*2*3
Final value of I is 3.

FOR I=1:1:3 WRITE I
To be printed: 123
Final value of I is 3.

FOR I=1:1:3,"ABC",2,4:2:7 WRITE " ",I
To be printed: 1 2 3 ABC 2 4 6
Final value of I is 6.

FOR I=1:1:5 SET:I=3 I=-6  SET:I=-3 I=9 WRITE " ",I
To be printed: 1 2 -6 -5 -4 9
Final value of I is 9.

Note that the fact that I receives a value smaller than the lower limit is no reason to terminate the loop.

Note that the fact that I receives a value greater than the upper limit is no reason to terminate execution of the scope of the FOR command.

FOR I=1:1:4 WRITE I FOR J=1:1:3 WRITE "*"
To be printed: 1***2***3***4***
Final value of I is 4, the final value of J is 3.

FOR I=3:5:0 WRITE I
Nothing is printed. Final value of I is 3.

SET X=1,Y=2,Z=7 FOR I=X:Y:Z WRITE I
To be printed: 1357

X   FOR I=1:1:3 FOR J=1:2:5 WRITE " ",I,J IF I=2 GOTO Y
    WRITE "Ready." QUIT
Y   WRITE ", jumped to Y." QUIT

When this routine is executed using the command DO X
will be printed: 11 13 15 21, jumped to Y.
The GOTO command terminates both FOR commands.

X   FOR I=1:1:3 FOR J=1:2:5 WRITE " ",I,J IF I=2 DO Z
    WRITE " Ready." QUIT
Y   WRITE " Y" QUIT
Z   WRITE " Z" GOTO Y

When this routine is executed using the command DO X
will be printed:
11 13 15 21 Z Y 23 Z  Y 25 Z Y 31 33 35 Ready.
The GOTO command in line Z has no implications for any FOR command.

SET K=0,L=0 FOR K=1:1:5,L=3:3:9 WRITE " ",K,L
To be printed: 10 20 30 40 50 00 30 60 90
Note that the value of L is not changed because of the FOR command. The expression L=3 is a truth valued expression with the value 0 (FALSE).

FOR K=1:1:3 FOR L=4:2:8 WRITE " ",K,L QUIT:L=6
To be printed: 14 16 24 26 34 36
The QUIT command only affects the FOR command with variable L as a counter.

SET STEP=2
FOR K=1:STEP:9 WRITE " ",K,"_",STEP SET STEP=STEP+K

To be printed: 1_2 3_3 5_6 7_11 9_18
Changing the value of local variable STEP has no implications for the step-size used by the M[UMPS]-system.

FOR K="A":1:4 WRITE " ",K
To be printed: 0 1 2 3 4
Note that the expression "A" will be evaluated as an nve (numeric valued expression), which causes it to be seen as a value of 0.

SET LIST="1*234*5*9876*abc*d*e*f*ghi*jkl",N=0
FOR I=1:1:$LENGTH(LIST,"*") SET N=N+1 WRITE !,N,": ",$PIECE(LIST,"*",I)

To be printed:
1: 1
2: 234
3: 5
4: 9876
5: abc
6: d
7: e
8: f
9: ghi
10: jkl

SET LIST="1*234*5*9876*abc*d*e*f*ghi*jkl",N=0
FOR I=1:1:$LENGTH(LIST,"*") SET N=N+1,I=I+1 QUIT:N>4  WRITE !,N,": ",$PIECE(LIST,"*",I)

To be printed:
1: 234
2: 9876
3: d
4: f

SET LIST="1*234*5*9876*abc*d*e*f*ghi*jkl",N=0
FOR I=1:1:$LENGTH(LIST,"*") SET N=N+1,I=I+1 QUIT:I>4  WRITE !,N,": ",$PIECE(LIST,"*",I)

To be printed:
1: 234
2: 9876

Typical code to iterate through a muli-level data structure would be:

SET sub1="" FOR  SET sub1=$ORDER(name(sub1)) QUIT:sub1=""  DO
 . SET sub2="" FOR  SET sub2=$ORDER(name(sub1,sub2)) QUIT:sub2=""  DO
 . . SET sub3="" FOR  SET sub3=$ORDER(name(sub1,sub2,sub3)) QUIT:sub3=""  DO
 . . . ; Perform some action at this level
 . . . QUIT
 . . QUIT
 . QUIT

Examples with naked references:

FOR LVN=FROM:STEP:UNTIL
SET ^ABC(1,2)="reset naked indicator"
; Naked indicator is now ^ABC(1,
FOR I=^(3,4):^(5,6):^(7,8)
; 1. fetch ^(3,4) = ^ABC(1,3,4)
; 2. fetch ^(5,6) = ^ABC(1,3,5,6)
; 3. fetch ^(7,8) = ^ABC(1,3,5,7,8)
; Naked indicator is now: ^ABC(1,3,5,7,


This document is © Ed de Moel, 1995-2005.
It is part of a book by Ed de Moel that is published under the title "M[UMPS] by Example" (ISBN 0-918118-42-5).
Printed copies of the book are no longer available.

This document describes the various commands that are defined in the M[UMPS] language standard (ANSI X11.1, ISO 11756).

The information in this document is NOT authoritative and subject to be modified at any moment.
Please consult the appropriate (draft) language standard for an authoritative definition.

In this document, information is included that will appear in future standards.
The MDC cannot guarantee that these 'next' standards will indeed appear.