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

M Tutorial (part 4/5)

 

MERGE <destination>=<source> or M <destination>=<source>

Copies into the <destination> array, global or variable the branch commencing at the <source> array, global or variable.

GLB014Þ;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 I,MEM
ÞI $D(^GLB) K ^GLB
Þ; Create a global
ÞF I=1:1:10 S ^GLB(I)=$C(64+I) ; Store a letter
ÞF I=1:2:10 S ^GLB(I,I)=$C(64+(I*2))
ÞM MEM=^GLB(1) ; Copy the global (and branches) to MEM
ÞW !,MEM," ",MEM(1)
ÞM ^GLB(100)=MEM
ÞW !,^GLB(100)," ",^GLB(100,1)
ÞQ

What we have done in GLB014 is to copy subscript 1 of ^GLB into a memory array MEM and further down we place into ^GLB(100) what had been placed in MEM.

The branch ^GLB(1) consisted of the following nodes: ^GLB(1)="A" and ^GLB(1,1)="B".

Following M MEM=^GLB(1) MEM will be defined as follows: MEM="A" and MEM(1)="B". GLB015 demonstrates what this command did, although appreciate that this command will automatically copy the entire branch is not in any way limited as is our example.

GLB015Þ;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 I,MEM
ÞI $D(^GLB) K ^GLB
Þ; Create a global
ÞF I=1:1:10 S ^GLB(I)=$C(64+I) ; Store a letter
ÞF I=1:2:10 S ^GLB(I,I)=$C(64+(I*2))
Þ; What M MEM=^GLB(1) did
ÞS MEM=^GLB(1)
ÞS MEM(1)=^GLB(1,1)
ÞW !,MEM," ",MEM(1)
ÞM ^GLB(100)=MEM
ÞW !,^GLB(100)," ",^GLB(100,1)
ÞQ

 

$NAME(<var>,<depth>) or $NA(<var>,<depth>)

Given that globals can have multiple subscripts, it may be tedious to have to key in the entire structure over and over again. M provides a facility by which one could refer to a global without having to type the entire branch. The shortcut version of the global reference is called its Naked Reference.

GLB016Þ;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 I,SUB1,SUB2,SUB3
ÞK ^GLB
ÞF I=1:1:5 D
Þ.S ^GLB(I)="LEVEL 1:"_I
Þ.S ^GLB(I,"A")="LEVEL 2:"_I
Þ.S ^GLB(I,"A","DATA")="LEVEL 3:"_I
Þ.Q
ÞW #
ÞS (SUB1,SUB2,SUB3)=""
10ÞS SUB1=$O(^GLB(SUB1)) Q:SUB1=""
ÞW !,SUB1," = ",^(SUB1)
20ÞS SUB2=$O(^(SUB1,SUB2)) G:SUB2="" 10
ÞW !,?20,SUB2," = ",^(SUB2)
30ÞS SUB3=$O(^(SUB2,SUB3)) G:SUB3="" 20
ÞW !,?40,SUB3," = ",^(SUB3)
ÞW !
ÞG 30

All colored areas are naked references. The syntax of a naked reference is ^(<subscripts>), where <subscripts> can be one or more subscripts separated by commas. Subscripts specified in the naked reference are appended to the right of the rightmost subscript of the Naked Indicator to form the full reference.

GLB017Þ;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 I,SUB1,SUB2,SUB3
ÞK ^GLB
ÞF I=1:1:5 D
Þ.S ^GLB(I)="LEVEL 1:"_I
Þ.S ^GLB(I,"A")="LEVEL 2:"_I
Þ.S ^GLB(I,"A","DATA")="LEVEL 3:"_I
Þ.Q
ÞW #
ÞS (SUB1,SUB2,SUB3)=""
10ÞS SUB1=$O(^GLB(SUB1)) Q:SUB1=""
ÞW !,$NA(^(SUB1))," = ",^(SUB1)
20ÞS SUB2=$O(^(SUB1,SUB2)) G:SUB2="" 10
ÞW !,?20,$NA(^(SUB2))," = ",^(SUB2)
30ÞS SUB3=$O(^(SUB2,SUB3)) G:SUB3="" 20
ÞW !,?40,$NA(^(SUB3))," = ",^(SUB3)
ÞW !
ÞG 30

In GLB017 uses the $NA function to expand the naked reference to the full global. It should be pointed out that use of naked references should not be encouraged because:

It makes understanding the program more difficult (image working with 6 globals within a chunk of code)

Updates to a program can introduce errors. There is one single naked reference indicator that will only hold within it the last reference. What happens if you place a new line of code referencing another global before the naked reference?

What will happen here? I '$D(^ABC(1)) S ^(1)=^XYZ(1,2). Will ^XYZ be set or ^ABC? (Hint: Try it out).

The fact that today the editors we use have copy/paste and find/replace facilities makes it easy to reproduce a the previous subscripts of the global.

GLB018Þ;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 I,SUB1,SUB2,SUB3
ÞK ^GLB
ÞF I=1:1:5 D
Þ.S ^GLB(I)="LEVEL 1:"_I
Þ.S ^GLB(I,"A")="LEVEL 2:"_I
Þ.S ^GLB(I,"A","DATA")="LEVEL 3:"_I
Þ.Q
ÞW #
ÞS (SUB1,SUB2,SUB3)=""
10ÞS SUB1=$O(^GLB(SUB1)) Q:SUB1=""
20ÞS SUB2=$O(^(SUB1,SUB2)) G:SUB2="" 10
30ÞS SUB3=$O(^(SUB2,SUB3)) G:SUB3="" 20
ÞW !,$NA(^GLB(SUB1,SUB2,SUB3),1)," = ",^GLB(SUB1)
ÞW !,?20,$NA(^GLB(SUB1,SUB2,SUB3),2)," = ",^GLB(SUB1,SUB2)
ÞW !,?40,$NA(^GLB(SUB1,SUB2,SUB3))," = ",^GLB(SUB1,SUB2,SUB3)
ÞW !
ÞG 30

The second (optional) parameter of $NAME is the number of subscripts we want to display. Notice that although we are still referencing the branch three levels deep we can use <depth> to only display the first <depth> subscripts.

Continued...

E&OE

1