Arrays: creation, access, insertion, deletion and sorting
Combines array operations while distinguishing shifting, length and copying.
Overview
Array positions start at 1. AIns shifts elements at unchanged length and discards the last; grow first with ASize to preserve it. ADel shifts left and leaves Nil at the end without shrinking; shrink afterwards when deletion should shorten the list. The consulted ASize page documents Nil as its return, so do not assign that return to the array. ASort modifies the supplied reference; use AClone first when preserving the original structure matters. The example combines documented operations from the related pages. (TOTVS, n.d., refs. 1–5)
Return value
No single return value: this is a composite workflow.
Commented example
#Include "Protheus.ch"
User Function EdArrays()
Local aValues := {30, 10, 20}
Local aCopy
// Insert without losing the last element.
ASize(aValues, Len(aValues) + 1)
AIns(aValues, 2)
aValues[2] := 15
ADel(aValues, 1)
ASize(aValues, Len(aValues) - 1)
aCopy := AClone(aValues)
ASort(aCopy)
ConOut(cValToChar(aValues[1]))
ConOut(cValToChar(aCopy[1]))
Return NilFinal original {15, 10, 20}; sorted copy {10, 15, 20}; expected outputs 15 and 10. Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.
- Check bounds before access. Distinguish array cloning from object copying and define a consistent comparator for heterogeneous structures.
- Under review: editorial example neither compiled nor run in Protheus. Check includes, release, RDD and context before testing.

Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.