|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Arrays and Data-Structures > ASA. - Associative Array > Assoc. - Array to/from Variable |
MiniRobotLanguage (MRL)
ASA.- Array to/from Variable Commands
Persist and Restore Associative Arrays
Overview
The ASSOC.-Array/to/from Variable commands enable the serialization and deserialization of an associative array (AVL tree) to and from a string variable.
This functionality allows you to save the state of an associative array into a string for storage or transmission and later restore it into a new or existing array.
These commands are particularly useful for persisting data across script executions or sharing data between different components.
A handle, generated by ASA.New, is used to identify the associative array. The stored string contains the entire structure and data of the array, which can be restored using the appropriate handle.
Serialization is the process of converting the associative array's structure and data into a string format. This string can be saved to a file, sent over a network, or stored in memory for later use.
Deserialization is the reverse process, where a previously serialized string is converted back into an associative array. The restored array retains the original key-value pairs and structure.
A handle is a unique identifier created when an associative array is initialized with ASA.New. It is used to reference the array during store and restore operations. The handle remains valid until the array is deallocated with ASA.End or ASA.EndAll.
•ASA.Store - Store the container to a string.
•ASA.Restore - Restore the container from a string.
This script demonstrates how to store an associative array into a string and restore it into a new array.
'***********************************
' ASSOC.-Array/to/from Variable Sample 1: Store and Restore
'***********************************
ASA.New|$$TRE|s
ASA.Set|$$TRE|Name|Alice
ASA.Set|$$TRE|Score|95
' Store the array to a string
ASA.Store|$$TRE|$$STR
DBP.Stored string: $$STR
' Deallocate the original array
ASA.End|$$TRE
' Create a new array and restore from the string
ASA.New|$$TRE2|s
ASA.Restore|$$TRE2|$$STR
' Verify the restored data
ASA.Get|$$TRE2|Name|$$VAL
DBP.Restored Name: $$VAL
ASA.Get|$$TRE2|Score|$$VAL
DBP.Restored Score: $$VAL
' Deallocate the restored array
ASA.End|$$TRE2
DBP.Array deallocated.
ENR.
This script shows how to pass a stored string between script executions and restore it.
'***********************************
' ASSOC.-Array/to/from Variable Sample 2: Cross-Script Use
'***********************************
' First part: Store the array
ASA.New|$$TRE|s
ASA.Set|$$TRE|Task|Pending
ASA.Set|$$TRE|Priority|High
ASA.Store|$$TRE|$$STR
SAV.Save|$$STR
ASA.End|$$TRE
DBP.Data stored for later use.
ENR.
' Second part: Restore the array (in a new script or later)
SAV.Restore|$$STR
ASA.New|$$TRE|s
ASA.Restore|$$TRE|$$STR
ASA.Get|$$TRE|Task|$$VAL
DBP.Restored Task: $$VAL
ASA.End|$$TRE
DBP.Array deallocated.
ENR.
The ASA.Store and ASA.Restore commands provide a convenient way to serialize and deserialize associative arrays.
This functionality is essential for saving state, sharing data, or recovering arrays after script interruptions.
The examples above illustrate practical applications of these commands in your scripts.
See also:
• ASA.New