|
<< 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.Got
Checks if a key exists in an associative array.
Intention
The ASA.Got command checks if a specific key exists in an associative array in SPR. You give it the array handle (like $$TRE) and a key (like $$KEY or "Name"), and optionally a result variable (like $$RES). It sets $$RES to "1" if the key exists, "0" if it doesn’t. This is a quick way to test for a key before using ASA.Get or ASA.Set, preventing errors or redundant operations.
Imagine a phonebook array—use ASA.Got to see if "Alice" is listed before adding her number. Or in a robot script, check if "Speed" exists before updating it. It’s a fast, reliable way to keep your script efficient and safe.
Illustration
🔍 Key Check: In an array with handle $$TRE, ASA.Got tests if "Name" exists.
✅ Result: $$RES gets "1" for yes, "0" for no—clear and simple!
Syntax
ASA.Got|$$TRE|$$KEY[|$$RES]
Parameter Explanation
P1 - $$TRE - (Variable, 5 characters max)
This is the handle of the array you’re checking. It’s a 5-character variable (like $$TRE or $$ARR) from ASA.New. The command uses this to access the array.
P2 - $$KEY - (Variable or String)
This is the key you’re looking for. It can be a variable (like $$KEY) or a string (like "Name"). SPR checks if this key exists in the array.
P3 - $$RES - (Optional, Variable, 5 characters max)
This is where the result goes. It’s an optional 5-character variable (like $$RES). SPR sets it to "1" if the key exists, "0" if it doesn’t. Skip this if you don’t need the result stored.
Examples
'***********************************
' ASA.Got - Sample 1: Check Key in String Array
'***********************************
ASA.New|$$TRE|s
ASA.Set|$$TRE|Alice|123-4567
ASA.Got|$$TRE|Alice|$$RES
MBX.Alice exists: $$RES (should be 1)
ASA.Got|$$TRE|Bob|$$RES
MBX.Bob exists: $$RES (should be 0)
MBX.Ready
'
'***********************************
' ASA.Got - Sample 2: Check Key in Quad Array
'***********************************
ASA.New|$$TRE|i
ASA.Set|$$TRE|Red|50
ASA.Got|$$TRE|Red|$$RES
PRT.Red exists: $$RES (should be 1)
ASA.Got|$$TRE|Blue|$$RES
PRT.Blue exists: $$RES (should be 0)
MBX.Ready
'
'***********************************
' ASA.Got - Sample 3: Check Key in Extended FP Array
'***********************************
ASA.New|$$TRE|f
ASA.Set|$$TRE|Speed|3.14
ASA.Got|$$TRE|Speed|$$RES
PRT.Speed exists: $$RES (should be 1)
ASA.Got|$$TRE|Temp|$$RES
PRT.Temp exists: $$RES (should be 0)
MBX.Ready
'
Remarks
- $$TRE must be valid—use ASA.Validate first if unsure.
- $$RES gets "1" if the key exists, "0" if it doesn’t—easy to test in scripts.
- Works for all datatypes (String, Quad, Extended)—keys must match array type.
- Faster than ASA.Get when you only need to know existence.
Limitations
- $$TRE must be 5 characters—wrong lengths might cause errors.
- If $$TRE isn’t valid, $$RES might be unreliable—no error warning.
- Doesn’t return the value—use ASA.Get for that.
See also:
• ASA.New
• ASA.Get
• ASA.Set