PSJ. - JSON Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > JSON - Parser > Parser-Operations > !Node Operations >

PSJ. - JSON Operations

PSJ.DupTree

PreviousTopNext


SPR Script Language

 

PSJ.DupTree

Duplicates a node and its subtree, linking it to a parent node.

 

Intention

 

The PSJ.DupTree command in SPR duplicates a node identified by $$SOURCE, including its entire subtree, and attaches it as a child to a parent node identified by $$PARENT). Optionally, it stores the identifier of the new root node in $$RET. It’s like telling your robot to copy a whole branch and its twigs, then graft it onto another tree in your JSON structure.

 

For example, duplicating node "101" (with its subtree) and linking it to parent "200" creates a new subtree under "200" with a new root ID.

 

Illustration

📝 Duplicate Subtree: PSJ.DupTree|101|200 duplicates node 101 and its subtree, linking it to node 200.
📝 With Result: PSJ.DupTree|102|300|$$NEW duplicates node 102’s subtree, links it to 300, and stores the new root ID in $$NEW.

 

Syntax

 

PSJ.DupTree|$$SOURCE|$$PARENT[|$$RET]

 

Parameter Explanation

 

P1 - $$SOURCE - (Variable, Numeric)

The identifier of the node to duplicate, including its subtree (e.g., $$SOURCE as "101"). Must be a valid node. Required.

 

P2 - $$PARENT - (Variable, Numeric)

The identifier of the parent node to attach the duplicated subtree to (e.g., $$PARENT as "200"). Use -1 if no parent is desired. Required.

 

P3 - $$RET - (Variable, Numeric, Optional)

The variable to store the identifier of the new root node (e.g., $$RET as "$$NEW"). Returns the new node ID or 0 if duplication fails. If omitted, the result is returned but not stored.

 

Examples

 

'***********************************

' Sample 1: Duplicate Subtree

PSJ.DupTree|101|200

' Duplicates node 101 and its subtree, attaching to node 200

MBX.Ready

'***********************************

' Sample 2: Duplicate and Store New ID

PSJ.DupTree|102|300|$$NEW

' Duplicates node 102’s subtree, attaches to 300, stores ID in $$NEW

IF $$NEW > 0 THEN PRT.New Root ID: $$NEW ELSE PRT.Error

MBX.Ready

 

Remarks

 

- $$RET returns the new root node ID (a positive value) or 0 if the duplication fails.

- Use PSJ.ErrCode for detailed error information if $$RET is 0.

 

Limitations

 

- $$SOURCE must be a valid node identifier.

- $$PARENT must be a valid node that can accept children (e.g., an object or array) or -1 for no parent.

 

See also:

 

PSJ.Clone

PSJ.ErrCode