Merges global nodes or subtrees from source into destination.
Description
MERGE destination=source copies source into destination and all descendants of source into descendants of destination. It does not modify source, or kill any nodes in destination.
MERGE simplifies the copying of a subtree (multiple subscripts) of a variable to another variable. Either variable can be a subscripted local variable, process-private global, or global. A subtree is all variables that are descendants of a specified variable. MERGE offers a one-command alternative to the current technique for doing subtree copy: a series of SET commands with $ORDER references.
MERGE issues a <COMMAND> error if the source and destination have a parent-child relationship.
Arguments
pc
An optional postconditional expression. InterSystems IRIS executes the MERGE command if the postconditional expression is true (evaluates to a nonzero numeric value). InterSystems IRIS does not execute the command if the postconditional expression is false (evaluates to zero). For further details, refer to Command Postconditional Expressions in Using ObjectScript.
destination and source
Variables to be merged. Either variable can be a local variable, a process-private global, or a global. If destination is undefined, MERGE defines it and sets it to source. If source is undefined, MERGE completes successfully, but does not change destination.
You can specify multiple, comma-separated destination=source pairs. They are evaluated in left-to-right order.
A mergeargument can reference a destination=source pair by indirection. For example, MERGE @tMergeString.
A mergeargument can be an array passed by reference that specifies a variable number of parameters, such in myargs...
The ^$GLOBAL SSVN can be the source variable. This copies the global directory to a destination variable. MERGE adds each global name as a destination subscript with a null value. This is shown in the following example:
MERGE gbls=^$GLOBAL("")
ZWRITE gbls
Examples
The following example copies a subtree from one global variable (^a) to another global variable (^b). In this case, the merge is being used to create a smaller global ^b, which contains only the ^a(1,1) subtree of the information in ^a.
SET ^a="cartoons"
SET ^a(1)="The Flintstones",^a(2)="The Simpsons"
SET ^a(1,1)="characters",^a(1,2)="place names"
SET ^a(1,1,1)="Flintstone family"
SET ^a(1,1,1,1)="Fred"
SET ^a(1,1,1,2)="Wilma"
SET ^a(1,1,2)="Rubble family"
SET ^a(1,1,2,1)="Barney"
SET ^a(1,1,2,2)="Betty"
MERGE ^b=^a(1,1)
WRITE ^b,!,^b(2),!,^b(2,1)," and ",^b(2,2)
The following example shows how a destination global variable looks after it has been merged with a subtree of a source global variable.
Suppose you execute the following:
KILL ^X,^Y
SET ^X(2,2)="first"
SET ^X(2,2,4)="second"
SET ^Y(3,6,7)="third"
SET ^Y(3,6,8)="fourth"
SET ^Y(3,6,7,8,4)="fifth"
SET ^Y(3,6,7,8,9)="sixth"
WRITE ^X(2,2),!,^X(2,2,4),!
WRITE ^Y(3,6,7),!,^Y(3,6,8),!
WRITE ^Y(3,6,7,8,4),!,^Y(3,6,7,8,9)
The following figure shows the resulting logical structure of ^X and ^Y.
Initial Structure of ^X and ^Y
Consider the following MERGE command:
MERGE ^X(2,3)=^Y(3,6,7,8)
When you issue the previous statement, InterSystems IRIS copies part of ^Y into ^X(2,3). The global ^X now has the structure illustrated in the figure below.
Result on ^X and ^Y of MERGE Command