Written by

Question Alan Nguyen · Mar 21, 2024

How to write a For Loop

Hello,

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

Comments

Chris Stewart · Mar 21, 2024

Something like this would do it

for i=1:1:$LENGTH(X,",") {

   w$PIECE(X,",",i),!
}
0
Robert Cemper · Mar 21, 2024
setX="1,4,6,8,9,12"for i=1:1:$L(X,",") set Y=$p(X,",",i) write Y,!
0
Alan Nguyen  Mar 21, 2024 to Robert Cemper

Thank you.  Can we use the variable X instead of using $L(X,”,”)?

0
Ben Spead  Mar 21, 2024 to Alan Nguyen

not directly - this is grabbing the Length ($Length()) of the variable X, using "," as a delimiter of the string.  So the counter 'i' will run from 1 to the length of X, and then each iteration will grab the next piece of the string (, delimited again)

0
Robert Cemper  Mar 22, 2024 to Alan Nguyen
setx="1,4,6,8,9,12"whilex>0 {write +x,! setx=$piece(x,",",2,*) }

x only !

0
Ben Spead  Mar 22, 2024 to Robert Cemper

creative!

0
Timo Lindenschmid · Mar 22, 2024

another option is to use a list

setx=$listfromstring("1,4,6,8,9,12")
   SET ptr=0WHILE$LISTNEXT(x,ptr,y) {
      WRITE !,y
   }
0
Michael Davidovich · Mar 22, 2024

You can also treat each integer as a string.  This would work if your list was small but would be annoying if you had a larger list. It's probably better suited for text strings and not integers treated as strings, but it is an option!

USER>for y="1","4","6","8","9","12" { w y,! }
1468912
0
Rich Taylor · Mar 22, 2024

Or use embedded Python

for x in [1,2,3,4,5,6,7,8]:
  print('this is x: '+repr(x))
0
Manel Trèmols · Apr 10, 2024

Similar to Robert solution but using for:

setx="1,4,6,8,9,12"forwrite !,+xsetx=$p(x,",",2,*) quit:x=""1468912
0
Steven Hobbs · Apr 10, 2024

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.

0
Julius Kavay · Apr 10, 2024

It seems, nobody likes the execute command... once celebrated, now frowned upon

setx="11,22,33,""Done"""xecute"for i="_x_" write i,!"112233
Done

😏

0