|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Arrays and Data-Structures > ASA. - Associative Array > ASSOC. - Associative Array |
MiniRobotLanguage (MRL)
ASA.- Operation Commands
Manage and manipulate Associative Arrays
Overview
The ASSOC.-Operation commands provide a set of tools to interact with and manipulate an associative array, also known as an AVL tree (Adelson-Velsky and Landis tree).
An associative array is a data structure that stores key-value pairs, maintaining balance to ensure efficient retrieval, insertion, and deletion operations.
These commands allow you to create, modify, traverse, and query the associative array, supporting various data types such as strings, integers, and floating-point numbers.
A handle is a unique identifier generated when an associative array is created using ASA.New. This handle is used to reference the array in subsequent commands. It becomes invalid once the array is deallocated with ASA.End or ASA.EndAll.
An associative array is a self-balancing binary search tree (AVL tree) that stores data as key-value pairs. It ensures efficient operations through automatic balancing, with the following characteristics:
•Each node contains a unique key and an associated value.
•The tree is self-balancing, maintaining a logarithmic height for optimal performance.
•Common uses include:
•Database indexing for quick data retrieval.
•Caching systems to store temporary key-value mappings.
•Implementing dictionaries or lookup tables.
A handle is a unique identifier that references a specific associative array. It is created when an array is initialized with ASA.New and is used to perform operations on that array. Handles enable the management of multiple associative arrays simultaneously. Once deallocated using ASA.End or ASA.EndAll, the handle becomes invalid.
A node is a reference to a specific key-value pair within the associative array. Commands like ASA.First, ASA.Last, ASA.Next, and ASA.Prev return node handles, which can be used with ASA.GetKey and ASA.GetVal to access the key and value of that node.
•ASA.Count - Get the number of items in the tree.
•ASA.Del - Remove a key and its associated value.
•ASA.First - Get the handle to the first node in the tree.
•ASA.Get - Get the value associated with a key.
•ASA.GetKey - Get the key of a node.
•ASA.GetVal - Get the value of a node.
•ASA.Got - Check if a key exists.
•ASA.Last - Get the handle to the last node in the tree.
•ASA.Next - Get the handle to the next node in the tree.
•ASA.Prev - Get the handle to the previous node in the tree.
•ASA.Set - Add a key/value to the tree.
•ASA.SetVal - Set the value of a node.
This script demonstrates how to create an associative array, add key-value pairs, retrieve values, and deallocate the array.
'***********************************
' ASSOC.-Operation Sample 1: Basic Operations
'***********************************
ASA.New|$$TRE|s
DBP.New array created with this Handle: $$TRE
' Add key-value pairs to the array
ASA.Set|$$TRE|Name|John
ASA.Set|$$TRE|Age|25
' Check the number of items
ASA.Count|$$TRE|$$COUNT
DBP.Number of items: $$COUNT
' Retrieve a value
ASA.Get|$$TRE|Name|$$VAL
DBP.Value for Name: $$VAL
' Remove a key-value pair
ASA.Del|$$TRE|Age
DBP.Age removed.
' Deallocate the array
ASA.End|$$TRE
DBP.Array deallocated.
ENR.
This script shows how to traverse the array using node handles to access keys and values.
'***********************************
' ASSOC.-Operation Sample 2: Traversing
'***********************************
ASA.New|$$TRE|s
ASA.Set|$$TRE|Key1|Value1
ASA.Set|$$TRE|Key2|Value2
ASA.Set|$$TRE|Key3|Value3
' Get the first node
ASA.First|$$TRE|$$NOD
ASA.GetKey|$$TRE|$$NOD|$$KEY
ASA.GetVal|$$TRE|$$NOD|$$VAL
DBP.First Key: $$KEY, Value: $$VAL
' Traverse to the next node
ASA.Next|$$TRE|$$NOD|$$NOD
ASA.GetKey|$$TRE|$$NOD|$$KEY
ASA.GetVal|$$TRE|$$NOD|$$VAL
DBP.Next Key: $$KEY, Value: $$VAL
' Deallocate the array
ASA.End|$$TRE
DBP.Array deallocated.
ENR.
This script demonstrates how to update the value of an existing node.
'***********************************
' ASSOC.-Operation Sample 3: Modifying Values
'***********************************
ASA.New|$$TRE|s
ASA.Set|$$TRE|ID|100
ASA.First|$$TRE|$$NOD
ASA.SetVal|$$TRE|$$NOD|200
ASA.Get|$$TRE|ID|$$VAL
DBP.Updated Value: $$VAL
' Deallocate the array
ASA.End|$$TRE
DBP.Array deallocated.
ENR.
The ASSOC.-Operation commands offer robust functionality for managing associative arrays.
These commands are ideal for applications requiring efficient key-value storage and retrieval, such as databases and caching systems.
The examples provided serve as a foundation for integrating associative arrays into your scripts.
See also:
• ASA.New
• ASA.Next