Assoc. - Array Clone

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Arrays and Data-Structures > ASA. - Associative Array >

Assoc. - Array Clone

ASA. - Array Clone Operation

Previous Top Next


MiniRobotLanguage (MRL)

 

ASA.- Array Clone Command

Create a Copy of an Associative Array

 

 

Overview

 
The ASA.Clone command creates a deep copy of an existing associative array (AVL tree).

This operation duplicates the entire structure and data of the source array, generating a new handle for the cloned array.

Cloning is useful for preserving the original data while allowing independent modifications to the copy, such as in scenarios requiring parallel data processing or backup creation.

 

A handle, created by ASA.New, identifies the source array, and a new handle is returned for the cloned array. Both handles remain valid until deallocated with ASA.End or ASA.EndAll.

 

Key Concepts

 

What is Cloning?

Cloning creates an independent duplicate of an associative array. Unlike a reference, changes to the cloned array do not affect the original, and vice versa, due to the deep copy nature of the operation.

 

What is a Handle?

A handle is a unique identifier generated when an associative array is created with ASA.New. The ASA.Clone command uses the source array's handle and returns a new handle for the cloned array. Both handles can be used independently until deallocated.

 

ASA. Array Clone Command

 

ASA.Clone - Clone the container.

 

Example Scripts

 

Example 1: Basic Cloning of an Associative Array

This script demonstrates how to clone an associative array and verify the independence of the original and cloned arrays.

 

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

' ASSOC.-Array Clone Sample 1: Basic Cloning

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

ASA.New|$$TRE|s

ASA.Set|$$TRE|User|Alice

ASA.Set|$$TRE|Score|85

 

' Clone the array

ASA.Clone|$$TRE|$$TRE2

DBP.Cloned array created with handle: $$TRE2

 

' Modify the original array

ASA.Set|$$TRE|Score|90

ASA.Get|$$TRE|Score|$$VAL

DBP.Original Score: $$VAL

 

' Check the cloned array

ASA.Get|$$TRE2|Score|$$VAL

DBP.Cloned Score: $$VAL

 

' Deallocate both arrays

ASA.End|$$TRE

ASA.End|$$TRE2

DBP.Arrays deallocated.

ENR.

 

Example 2: Using Cloned Array for Parallel Processing

This script shows how to use a cloned array for independent modifications while preserving the original data.

 

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

' ASSOC.-Array Clone Sample 2: Parallel Processing

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

ASA.New|$$TRE|s

ASA.Set|$$TRE|Item|A

ASA.Set|$$TRE|Quantity|10

 

' Clone the array

ASA.Clone|$$TRE|$$TRE2

DBP.Cloned array created.

 

' Modify the original array

ASA.Set|$$TRE|Quantity|15

ASA.Get|$$TRE|Quantity|$$VAL

DBP.Original Quantity: $$VAL

 

' Modify the cloned array

ASA.Set|$$TRE2|Quantity|20

ASA.Get|$$TRE2|Quantity|$$VAL

DBP.Cloned Quantity: $$VAL

 

' Deallocate both arrays

ASA.End|$$TRE

ASA.End|$$TRE2

DBP.Arrays deallocated.

ENR.

 

Summary

The ASA.Clone command provides a powerful way to duplicate associative arrays, enabling independent data manipulation.

This is particularly valuable for scenarios requiring data preservation or parallel processing.

The examples above demonstrate practical uses of cloning in your scripts.

 

See also:

 

ASA.Restore

ASA.New

ASA.FileStore

ASA.Clear