ARS. - Array Operations

<< Click to Display Table of Contents >>

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

ARS. - Array Operations

 

📘 SPR Array Commands Manual

Welcome to the SPR Array Commands Manual! 🌟

This guide provides a comprehensive overview of Arrays in SPR, explaining what they are, how to use them, and why they are a powerful tool for scripting. Whether you're a beginner or an experienced user, this manual will help you master Arrays with clear explanations and practical examples. Let’s dive in! 🚀

 

📄 Table of Contents

What is an Array?

Array Commands Overview

Code Examples

 

What is an Array? 📜

An Array in SPR is a dynamic, ordered container that can store multiple pieces of data, such as numbers, text, or more complex objects. Think of it as a flexible list or a sequence of elements that you can manipulate in various ways.

Arrays are dynamic, meaning they can grow or shrink as you add or remove items. They can hold different types of data, including:

1.Strings (e.g., "Hello")

2.Integers (e.g., 123) - 64-bit numbers with a range of -9.22x10^18 to 9.22x10^18

3.Floats (e.g., 3.14) - Floating-point numbers with 18 digits of precision

 

Array Commands Overview 🛠️

Here’s a quick overview of the most common ARS commands:

 

Container Management

 

' Creates a new Array.

ARS.New|$$ARS[|Type]

 

' Frees the Array from memory.

ARS.End|$$ARS

 

' Removes all items from the Array.

ARS.Clear|$$ARS

 

' Returns the number of items in the Array.

ARS.Count|$$ARS|$$RES

 

Adding and Removing Items

 

' Adds an item to the end of the Array.

ARS.Add|$$ARS|Value

 

' Inserts an item at the beginning of the Array.

ARS.Insert|$$ARS|Value

 

' Removes the item at the specified node.

ARS.Delete|$$ARS|$$NOD

 

Accessing Items

' Retrieves the value of a specific node.

ARS.Get|$$ARS|$$NOD|$$RES

' Updates the value of a specific node.

ARS.Set|$$ARS|$$NOD|Value

 

Advanced Operations

' Creates a copy of the Array.

ARS.Clone|$$ARS|$$RES

 

' Reverses the order of items in the Array.

ARS.Reverse|$$ARS

 

' Sorts the items in the Array.

ARS.Sort|$$ARS

 

' Sorts the Array and removes duplicates.

ARS.UniqueSort|$$ARS

 

' Validates the Array.

ARS.Validate|$$ARS

 

Code Examples

Here are some practical examples to help you understand how to use ARS commands in your scripts:

 

Example 1: Basic Array Operations

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

' Basic Array Operations

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

ARS.New|$$ARS

ARS.Add|$$ARS|'Hello'

ARS.Add|$$ARS|'World'

ARS.Count|$$ARS|$$RES

DBP.Number of items: $$RES

ARS.Get|$$ARS|1|$$VAL

DBP.First item: $$VAL

ARS.End|$$ARS

'

 

Example 2: Sorting and Reversing

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

' Sorting and Reversing

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

ARS.New|$$ARS

ARS.Add|$$ARS|3

ARS.Add|$$ARS|1

ARS.Add|$$ARS|2

ARS.Sort|$$ARS

ARS.Reverse|$$ARS

ARS.Get|$$ARS|1|$$VAL

DBP.First item after sorting and reversing: $$VAL

ARS.End|$$ARS

'

 

Example 3: Cloning an Array

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

' Cloning an Array

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

ARS.New|$$ARS

ARS.Add|$$ARS|'Apple'

ARS.Add|$$ARS|'Banana'

ARS.Add|$$ARS|'Cherry'

ARS.Clone|$$ARS|$$CLONE

ARS.Get|$$CLONE|1|$$VAL

DBP.First item in cloned array: $$VAL

ARS.End|$$ARS

ARS.End|$$CLONE

'

 

Example 4: Using Stack Operations

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

' Using Stack Operations

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

ARS.New|$$ARS

ARS.StkPush|$$ARS|'First'

ARS.StkPush|$$ARS|'Second'

ARS.StkPeek|$$ARS|$$VAL

DBP.Top item: $$VAL

ARS.StkPop|$$ARS|$$VAL

DBP.Popped item: $$VAL

ARS.End|$$ARS

'

 

Example 5: Using Queue Operations

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

' Using Queue Operations

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

ARS.New|$$ARS

ARS.QuePush|$$ARS|'First'

ARS.QuePush|$$ARS|'Second'

ARS.QuePeek|$$ARS|$$VAL

DBP.First item: $$VAL

ARS.QuePop|$$ARS|$$VAL

DBP.Popped item: $$VAL

ARS.End|$$ARS

'

 

Example 6: Using Deque Operations

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

' Using Deque Operations

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

ARS.New|$$ARS

ARS.PushFirst|$$ARS|'First'

ARS.PushLast|$$ARS|'Last'

ARS.PeekFirst|$$ARS|$$VAL

DBP.First item: $$VAL

ARS.PeekLast|$$ARS|$$VAL

DBP.Last item: $$VAL

ARS.PopFirst|$$ARS|$$VAL

DBP.Popped first item: $$VAL

ARS.PopLast|$$ARS|$$VAL

DBP.Popped last item: $$VAL

ARS.End|$$ARS

'

 

Example 7: Storing and Restoring from String

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

' Storing and Restoring from String

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

ARS.New|$$ARS

ARS.Add|$$ARS|'Data1'

ARS.Add|$$ARS|'Data2'

ARS.Store|$$ARS|$$STRING

DBP.Stored string: $$STRING

ARS.End|$$ARS

ARS.New|$$ARS

ARS.Restore|$$ARS|$$STRING

ARS.Get|$$ARS|1|$$VAL

DBP.First item after restoring: $$VAL

ARS.End|$$ARS

'

 

Example 8: Storing and Restoring from File

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

' Storing and Restoring from File

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

ARS.New|$$ARS

ARS.Add|$$ARS|'Data1'

ARS.Add|$$ARS|'Data2'

ARS.FileStore|$$ARS|'C:\ArrayData.dat'

DBP.Array stored to file.

ARS.End|$$ARS

ARS.New|$$ARS

ARS.FileRestore|$$ARS|'C:\ArrayData.dat'

ARS.Get|$$ARS|1|$$VAL

DBP.First item after restoring: $$VAL

ARS.End|$$ARS

'

 

Example 9: Building a String

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

' Building a String

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

ARS.New|$$ARS

ARS.Add|$$ARS|'Hello'

ARS.Add|$$ARS|'World'

ARS.BuildStr|$$ARS|$$STRING

DBP.Built string: $$STRING

ARS.End|$$ARS

'

 

Example 10: Loading and Saving Text Files

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

' Loading and Saving Text Files

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

ARS.New|$$ARS

ARS.TextLoad|$$ARS|'C:\example.txt'

ARS.Get|$$ARS|1|$$VAL

DBP.First line: $$VAL

ARS.TextSave|$$ARS|'C:\example_copy.txt'

DBP.Text file saved.

ARS.End|$$ARS

'

 

Example 11: Loading Files in a Folder

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

' Loading Files in a Folder

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

ARS.New|$$ARS

ARS.GetFiles|$$ARS|'C:\Folder'|'*.txt'

ARS.Get|$$ARS|1|$$VAL

DBP.First file: $$VAL

ARS.End|$$ARS

'

 

Example 12: Loading Full Paths of Files in a Folder

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

' Loading Full Paths of Files in a Folder

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

ARS.New|$$ARS

ARS.GetPaths|$$ARS|'C:\Folder'|'*.txt'

ARS.Get|$$ARS|1|$$VAL

DBP.First file path: $$VAL

ARS.End|$$ARS

'

 

Example 13: Binary Search and Insert

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

' Binary Search and Insert

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

ARS.New|$$ARS

ARS.Add|$$ARS|10

ARS.Add|$$ARS|20

ARS.Add|$$ARS|30

ARS.BinSearch|$$ARS|20|$$IND

DBP.Index of 20: $$IND

ARS.BinInsert|$$ARS|25

ARS.Get|$$ARS|3|$$VAL

DBP.Value at index 3 after insert: $$VAL

ARS.End|$$ARS

'

 

Example 14: Setting String Comparison

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

' Setting String Comparison

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

ARS.New|$$ARS

ARS.Comparison|$$ARS|1|'en_US'

ARS.Add|$$ARS|'apple'

ARS.Add|$$ARS|'Apple'

ARS.Sort|$$ARS

ARS.Get|$$ARS|1|$$VAL

DBP.First item after sorting: $$VAL

ARS.End|$$ARS

'

 

Example 15: Validating an Array

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

' Validating an Array

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

ARS.New|$$ARS

ARS.Validate|$$ARS|$$RES

DBP.Array is valid: $$RES

ARS.End|$$ARS

'

 

Example 16: Clearing an Array

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

' Clearing an Array

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

ARS.New|$$ARS

ARS.Add|$$ARS|'Data1'

ARS.Add|$$ARS|'Data2'

ARS.Clear|$$ARS

ARS.Count|$$ARS|$$RES

DBP.Number of items after clearing: $$RES

ARS.End|$$ARS

'

 

Example 17: Getting Array Type

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

' Getting Array Type

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

ARS.New|$$ARS|'s'

ARS.GetType|$$ARS|$$TYP

DBP.Array type: $$TYP

ARS.End|$$ARS

'

 

Example 18: Advanced Array Operations

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

' Advanced Array Operations

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

ARS.New|$$ARS

ARS.Add|$$ARS|5

ARS.Add|$$ARS|3

ARS.Add|$$ARS|8

ARS.Add|$$ARS|1

ARS.Sort|$$ARS

ARS.UniqueSort|$$ARS

ARS.Reverse|$$ARS

ARS.Get|$$ARS|1|$$VAL

DBP.First item after advanced operations: $$VAL

ARS.End|$$ARS

'

 

Example 19: Complex Data Manipulation

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

' Complex Data Manipulation

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

ARS.New|$$ARS

ARS.Add|$$ARS|'Item1'

ARS.Add|$$ARS|'Item2'

ARS.Add|$$ARS|'Item3'

ARS.Ins|$$ARS|2|'NewItem'

ARS.Del|$$ARS|3

ARS.Get|$$ARS|2|$$VAL

DBP.Item at index 2 after manipulation: $$VAL

ARS.End|$$ARS

'

 

Example 20: Integrated Example

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

' Integrated Example

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

ARS.New|$$ARS

ARS.Add|$$ARS|'Data1'

ARS.Add|$$ARS|'Data2'

ARS.Store|$$ARS|$$STRING

DBP.Stored string: $$STRING

ARS.End|$$ARS

ARS.New|$$ARS

ARS.Restore|$$ARS|$$STRING

ARS.Get|$$ARS|1|$$VAL

DBP.First item after restoring: $$VAL

ARS.FileStore|$$ARS|'C:\ArrayData.dat'

DBP.Array stored to file.

ARS.End|$$ARS

ARS.New|$$ARS

ARS.FileRestore|$$ARS|'C:\ArrayData.dat'

ARS.Get|$$ARS|1|$$VAL

DBP.First item after restoring from file: $$VAL

ARS.End|$$ARS

'

 

Remarks

These examples demonstrate the versatility and power of ARS commands in SPR. You can use these commands to manage arrays, perform various operations, and integrate them into your scripts for efficient data handling.

 

Limitations

- Ensure that the array is properly initialized before performing operations.

- Be cautious with data types and ensure consistency when adding and manipulating items.

 

See also

 

ARS.New

ARS.Add

ARS.Sort