|
<< Click to Display Table of Contents >> Navigation: 3. Script Language > Arrays and Data-Structures > ASA. - Associative Array > Assoc. Array Operation > ASA. - Associative Array Operations |
SPR Script Language
ASA.Count
Returns the number of key-value pairs in an associative array.
Intention
The ASA.Count command tells you how many items are in your associative array in SPR. You give it a handle (like $$TRE), and it counts all the key-value pairs—like "Name" → "Alice" or "Speed" → "2.5"—and puts the total in a variable like $$RES. This is perfect for keeping track of how much data you’ve got, whether you’re managing a phonebook, robot settings, or inventory. It helps you decide things like whether to add more entries or clear the array with ASA.Clear if it’s getting too full.
For example, if you’ve got a phonebook array, ASA.Count tells you how many contacts are in there—maybe 5 if you’ve added "Alice," "Bob," and a few others. Or in a robot script, it counts how many sensor readings you’ve stored (like "Temp" → "23.5", "Pressure" → "101.3"). It’s quick and simple, giving you a number to work with so you know what’s going on in your array.
Illustration
📏 Count Items: An array with handle $$TRE has pairs like "Name" → "Alice" and "Age" → "25"—ASA.Count says there are 2.
🔢 Result: The total goes into $$RES, so you know how full your array is!
Syntax
ASA.Count|$$TRE[|$$RES]
Parameter Explanation
P1 - $$TRE - (Variable, 5 characters max)
This is the handle of the array you want to count. It’s a 5-character variable (like $$TRE or $$ARR) from ASA.New. The command looks at this array and counts its pairs.
P2 - $$RES - (Optional, Variable, 5 characters max)
This is where the count goes. It’s an optional 5-character variable (like $$RES). SPR puts a number here—0 if the array is empty, or higher if there are pairs. You can skip this if you don’t need the count saved.
Examples
'***********************************
' ASA.Count - Sample 1: Count a String Array
'***********************************
ASA.New|$$TRE|s
ASA.Set|$$TRE|Alice|123-4567
ASA.Set|$$TRE|Bob|987-6543
ASA.Count|$$TRE|$$RES
MBX.Phonebook has $$RES entries (should be 2)
MBX.Ready
'
'***********************************
' ASA.Count - Sample 2: Count After Clearing a Quad Array
'***********************************
ASA.New|$$TRE|i
ASA.Set|$$TRE|Red|50
ASA.Set|$$TRE|Blue|75
ASA.Clear|$$TRE
ASA.Count|$$TRE|$$RES
PRT.Array count after clear: $$RES (should be 0)
MBX.Ready
'
'***********************************
' ASA.Count - Sample 3: Count an Extended FP Array
'***********************************
ASA.New|$$TRE|f
ASA.Set|$$TRE|Temp|23.5
ASA.Set|$$TRE|Speed|3.14
ASA.Set|$$TRE|Depth|10.2
ASA.Count|$$TRE|$$RES
PRT.Sensor readings count: $$RES (should be 3)
MBX.Ready
'
Remarks
- $$TRE must be valid—use ASA.Validate first if you’re unsure.
- $$RES gets 0 for an empty array (e.g., after ASA.Clear), or a positive number based on how many pairs are there.
- It’s fast and doesn’t change the array—just counts what’s in it.
- Works the same for all datatypes (String, Quad, Extended) from ASA.New.
Limitations
- $$TRE must be 5 characters—wrong lengths might break the command.
- If $$TRE isn’t valid, you might get junk in $$RES—no error warning.
- Without $$RES, you won’t see the count unless you check another way.
See also:
• ASA.New
• ASA.Set