M Web Magazine 007 (June 5, 1998 - September 4, 1998)

M Tutorial (part 2/5)

Another observation is that a particular array cannot have two identical subscripts. The data of the first one will be overwritten by the data of the second one (as was the case with variables).

GLB005Þ;Program demonstrates arrays and globals - Chris Bonnici - Mar 1998
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞN ARR,SUBS,DATA,I,ANS,DIR
ÞF I=1:1:10 D
Þ.R !!,"Enter Subscript :",SUBS
Þ.R !,"Enter Data :",DATA
Þ.S ARR(SUBS)=DATA
Þ.Q
Þ; where do we go from here?
ÞW !!
ÞS SUBS=5,DIR=1
ÞF S SUBS=$O(ARR(SUBS),DIR) D Q:ANS="Q"
Þ.I SUBS'="" W !,"Subscript ",SUBS," = ",ARR(SUBS)
Þ.E W !,$S(DIR=-1:"Beginning",1:"End")_" of array"
Þ.F R ", <U>p, <D>own or <Q>uit: ",ANS#1 Q:"UDQ"[ANS ; Loop until users enters a valid character
Þ.Q:ANS="Q" ; bye
Þ.S DIR=$S(ANS="U":-1,1:1)
Þ.Q
ÞQ

GLB005 uses the same initial stub as GLB004 to fill the array, but this should demonstrate the flexibility available in M with regards to traversing the array. One interesting point is the position at which we enter the array. It does not necessarily have to the be at the beginning. Assuming, for example that subscripts are 0-9, this program stars off at the first subscript after 5. Recalling that $O gives the next subscript after what is entered, ARR(5) need not be defined.mwmtryit.gif (2244 bytes)

Try creating a few arrays of your own to get the hang of it. Ensure that you understand how to traverse the array forward and backward and how to enter upon a particular value.

 

$GET(<var>,<default>) or $G(<var>,<default>)

This function returns the value of the variable if it exists, otherwise it returns <default>. Do appreciate that this function works with arrays as much at it works with variables and is very often used in those situations in which the one is uncertain whether a variables exists.

GLB006Þ;Program demonstrates arrays and globals - Chris Bonnici - Mar 1998
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞN ARR,I
ÞF I=1:1:10 S ARR($R(20))="TAKEN"
ÞW #
ÞF I=0:1:19 W !,$G(ARR(I),"N/A")
ÞQ

Whenever ARR(I) is defined, the value will be printed, otherwise the default text will be printed. GLB007 performs exactly the same function using functions we had covered last time.

GLB007Þ;Program demonstrates arrays and globals - Chris Bonnici - Mar 1998
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞN ARR,I
ÞF I=1:1:10 S ARR($R(20))="TAKEN"
ÞW #
ÞF I=0:1:19 W !,$S($D(ARR(I)):ARR(I),1:"N/A")
ÞQ

mwmtryit.gif (2244 bytes)Dry run the following (direct mode):

K
S A=10,B=""
W $G(A,"A Not Defined")," ",$G(B,"B Not Defined")," ",$G(C,"C Not Defined")

 

More Subscripts

Up to now we have limited ourselves to one subscript. In M, each element can be defined by more than one subscript.

GLB008Þ;Program demonstrates arrays and globals - Chris Bonnici - Mar 1998
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞN ARR,I,LET,SUB1,SUB2,SUB3
ÞF I=1:1:5 D
Þ.S ARR(I)="LEVEL 1:"_I
Þ.S ARR(I,"A","DATA")="LEVEL 3:"_I
Þ.Q
ÞW #
ÞS (SUB1,SUB2,SUB3)=""
10ÞS SUB1=$O(ARR(SUB1)) Q:SUB1=""
20ÞS SUB2=$O(ARR(SUB1,SUB2)) G:SUB2="" 10
30ÞS SUB3=$O(ARR(SUB1,SUB2,SUB3)) G:SUB3="" 20
ÞW !!,SUB1," ",SUB2," ",SUB3
ÞW !,ARR(SUB1)
ÞW !,ARR(SUB1,SUB2,SUB3)
ÞG 30

GLB008 sets up an multi-subscript array. The diagram shown below demonstrates diagrammatically how the array looks although it must be clarified that the coloured cells do not actually exist. In order to traverse the entire structure one must progressively $ORDER on each subscript.

Although it is not difficult, some people find the $ORDER somewhat difficult to comprehend. Just keep in mind that it returns the value of the next subscript at that level.

GLB009Þ;Program demonstrates arrays and globals - Chris Bonnici - Mar 1998
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞN ARR,I,LET,SUB1,SUB2,SUB3
ÞF I=1:1:5 D
Þ.S ARR(I)="LEVEL 1:"_I
Þ.S ARR(I,"A","DATA")="LEVEL 3:"_I
Þ.Q
ÞW #
ÞS (SUB1,SUB2,SUB3)=""
10ÞS SUB1=$O(ARR(SUB1)) Q:SUB1=""
ÞW !!,SUB1
ÞW !,ARR(SUB1)
ÞG 10

In GLB009 we still create the structure shown above, but this time we display go through the outermost subscript. Compare this program to the previous one and note that we removed labels 20 and 30 (plus other necessary, yet minor adjustments). Suppose I want the values with the 3rd subscript equal to "DATA"? You can only arrive at the nth subscript if you had first determined all the subscripts before it. GLB010 (a modified version of GLB008) only prints the "DATA" values.

GLB010Þ;Program demonstrates arrays and globals - Chris Bonnici - Mar 1998
Þ;M Web Magazine @ http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞN ARR,I,LET,SUB1,SUB2,SUB3
ÞF I=1:1:5 D
Þ.S ARR(I)="LEVEL 1:"_I
Þ.S ARR(I,"A","DATA")="LEVEL 3:"_I
Þ.Q
ÞW #
ÞS (SUB1,SUB2,SUB3)=""
10ÞS SUB1=$O(ARR(SUB1)) Q:SUB1=""
20ÞS SUB2=$O(ARR(SUB1,SUB2)) G:SUB2="" 10
30ÞS SUB3=$O(ARR(SUB1,SUB2,SUB3)) G:SUB3="" 20
WÞ!!,SUB1," ",SUB2," ",SUB3
WÞ!,ARR(SUB1,SUB2,SUB3)
ÞG 30

Continued...

E&OE

1