· Mar 21

How to write a For Loop


I have a variables X="1,4,6,8,9,12"  and I want to write a For loop with this variable in similar with the loop below:

For Y=1,4,6,8,9,12 {

  Write Y,!


Can someone help?  Thank you.

Product version: IRIS 2021.2
Discussion (13)5
Log in or sign up to continue

Most of the ObjectScript examples above (but not the Python example) take O(n**2) time (i.e., quadratic time) where 'n' is number of values in X.  This happens because the $PIECE(X,",",i) starts searching from the beginning of the string or because shortening the string X with

set x=$p(x,",",2,*)

will copy the string 'x' multiple times with the first element deleted before each copy.

The ObjectScript example from Timo Lindenschmid  using $LISTNEXT takes O(n) time (linear time).  This can be important if $L(X,",") is 1000 or more because such a list with 1000 elements will require about 500,000 scans of "," characters or about 500,000 copies of string characters.

Rich Taylor's Python example should also take linear time.

Another solution would be to build a %DynamicArray object and then iterate through its contents.

Finally, you could build a multi-dim ObjectScript array and iterate through the subscripts with $ORDER.  This is method closest to using standard MUMPS/ANSI M/ObjectScript features.