Array -Commands

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Arrays and Data-Structures > Array -Commands > !String-Arrays >

Array -Commands

ARR.Delete

Previous Top Next


MiniRobotLanguage (MRL)

 

ARR.Delete

Delete an Element at a Specified Index in an Array

clip_delete_array

Delete an element at a specified index in an array, shifting remaining elements and reducing the array size.

 

Intention

The ARR.Delete command removes the element at a specified index (P2) from a specified array (P1), shifting all subsequent elements to the left to fill the gap and reducing the array’s size by one. This command is the opposite of ARR.Insert and is useful for dynamically modifying arrays, such as removing unwanted elements or maintaining sorted lists.

The command requires exactly two parameters: the array number and the index of the element to delete. It modifies the array in-place and returns a status code (1 for success, 0 for failure). The operation works with any array type (string, integer, or floating-point), as all arrays are stored as strings internally in MRL.

Element Shift: Elements after the specified index are shifted left to fill the gap.

Array Resize: The array size is reduced by one after deletion.

Status Code: Returns 1 for success or 0 for failure via SL_27.

 

Schematic (Array Deletion)

Array[1]: ["Apple", "Banana", "Orange"]

Command: ARR.Delete|1|1

Result: Array[1]: ["Apple", "Orange"]

 

Syntax

ARR.Delete|P1|P2

 

Parameter Explanation

 

P1 - Array Number: Specifies the array number, which can range from 0 to 32.

P2 - Index: Specifies the 0-based index of the element to delete. Must be a valid non-negative integer within the array’s bounds.

 

Examples

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

' Example 1: Delete an element from a string array

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

ARR.Set|1|0|Apple

ARR.Set|1|1|Banana

ARR.Set|1|2|Orange

ARR.Delete|1|1

' Array 1 now contains: ["Apple", "Orange"]

ARR.Get|1|1|$$RET

MBX.$$RET

ENR.

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

' Example 2: Delete the first element from an integer array

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

ARR.Set INT|2|0|123

ARR.Set INT|2|1|456

ARR.Delete|2|0

' Array 2 now contains: ["456"]

ARR.Get|2|0|$$RET

MBX.$$RET

ENR.

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

' Example 3: Delete the last element from a floating-point array

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

ARR.Set FP|3|0|1.23

ARR.Set FP|3|1|4.56

ARR.Delete|3|1

' Array 3 now contains: ["1.23"]

ARR.Get|3|0|$$RET

MBX.$$RET

ENR.

'============================================================

' SELF-VALIDATING TEST SCRIPT for ARR.Delete

' Purpose: Verify functionality with JIV. for automated checks.

' Tests deleting elements, invalid indices, invalid array numbers, and invalid parameters.

'============================================================

' Initialize counters

$$PAS=0

$$FAI=0

STS.CLEAR

PRT. ===================================================

PRT. Test 1.1: Delete an element from a string array

STS.CLEAR

ARR.Clr|1

ARR.Set|1|0|Apple

ARR.Set|1|1|Banana

ARR.Set|1|2|Orange

ARR.Delete|1|1

ARR.Get|1|1|$$RET

$$EXP=Orange

JIV.$$RET!$$EXP|Lab_Error1

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next1

:Lab_Error1

GSB.Test

'-----------------------------------------------------------

:Lab_Next1

PRT. Test 1.2: Delete the last element

STS.CLEAR

ARR.Clr|2

ARR.Set|2|0|Mango

ARR.Set|2|1|Grape

ARR.Delete|2|1

ARR.Get|2|0|$$RET

$$EXP=Mango

JIV.$$RET!$$EXP|Lab_Error2

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next2

:Lab_Error2

GSB.Test

'-----------------------------------------------------------

:Lab_Next2

PRT. Test 1.3: Invalid index (negative)

STS.CLEAR

ARR.Clr|3

ARR.Set|3|0|Test

ARR.Delete|3|-1

ARR.Get|3|0|$$RET

$$EXP=Test

JIV.$$RET!$$EXP|Lab_Error3

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next3

:Lab_Error3

GSB.Test

'-----------------------------------------------------------

:Lab_Next3

PRT. Test 1.4: Invalid index (beyond array size)

STS.CLEAR

ARR.Clr|4

ARR.Set|4|0|Test

ARR.Delete|4|10

ARR.Get|4|0|$$RET

$$EXP=Test

JIV.$$RET!$$EXP|Lab_Error4

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next4

:Lab_Error4

GSB.Test

'-----------------------------------------------------------

:Lab_Next4

PRT. Test 1.5: Invalid array number (negative)

STS.CLEAR

ARR.Clr|5

ARR.Set|5|0|Test

ARR.Delete|-1|0

ARR.Get|5|0|$$RET

$$EXP=Test

JIV.$$RET!$$EXP|Lab_Error5

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next5

:Lab_Error5

GSB.Test

'-----------------------------------------------------------

:Lab_Next5

PRT. Test 1.6: Invalid parameter count (too few)

STS.CLEAR

ARR.Clr|6

ARR.Set|6|0|Test

ARR.Delete|6

ARR.Get|6|0|$$RET

$$EXP=Test

JIV.$$RET!$$EXP|Lab_Error6

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next6

:Lab_Error6

GSB.Test

:Lab_Next6

PRT. ===================================================

PRT. TEST SUMMARY

PRT. ===================================================

CAL.$$TOT=$$PAS+$$FAI

$$MSG=Total Tests: $$TOT

PRT.$$MSG

$$MSG=Passed: $$PAS

PRT.$$MSG

$$MSG=Failed: $$FAI

PRT.$$MSG

JIV.$$FAI=0|Lab_Success

$$MSG=FAILURE: $$FAI of $$TOT tests failed.

MBX.$$MSG|Test Result|16

JMP.Lab_End

:Lab_Success

MBX.SUCCESS: All tests passed!|Test Result|64

:Lab_End

ENR.

:Test

$$MSG= -> FAIL - Result: $$RET (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

RET.

 

Remarks

- The command shifts elements after the deleted index to the left, reducing the array size by one, in contrast to ARR.Insert, which shifts elements right.

- Arrays are stored as strings internally, allowing deletion from any array type (string, integer, or floating-point).

- The command returns a status code (1 for success, 0 for failure) via SL_27, indicating whether the deletion was successful.

- Invalid indices or array numbers result in no operation and a status code of 0.

 

Limitations

- Array numbers are limited to 0–32, consistent with other MRL array commands.

- Invalid array numbers (e.g., negative or >32) or invalid indices (e.g., negative or beyond array size) result in no operation.

- The command requires exactly two parameters; fewer or more parameters result in no operation.

- No support for multidimensional arrays; use other commands for complex array structures.

 

See also:

    ARR.Set Array

    Get Array

    Clr Array

    Dim Array

    ARR.Insert

    ARR.Swap