Usina BRAdvPL Guide
Sign in
← All topics
FUNCTIONPublished

AAdd

AAdd( <aDestino>, <xExpressao> ) → xExpressao

Appends one element to an array and increases its size by one position.

ArrayMatrixCollectionsAdvPL functions
01 · OVERVIEW

Overview

AAdd() appends the value of any valid AdvPL expression to an array and returns that value. It is useful for lists, queues and matrix rows whose size is not known in advance.

02 · SYNTAX

Syntax

AAdd( <aDestino>, <xExpressao> ) → xExpressao

Parameters

aDestino
ArrayRequired

Array that receives the new last element and is resized.

xExpressao
QualquerRequired

Value assigned to the new position; it may also be another array.

Return value

Returns the value supplied in xExpression, not necessarily the destination array.

03 · PRACTICAL EXAMPLE

Appending values to a vector

User Function ExAAddNames()
    Local aNames := {}

    AAdd(aNames, "Alice")
    AAdd(aNames, "Bruno")
    AAdd(aNames, "Carla")

    MsgInfo(aNames[3], "Third name")
Return
Expected result

The array contains three values and position 3 contains “Carla”.

04 · PRACTICAL EXAMPLE

Building a matrix

// See the Portuguese example; localize only user-facing text.
Expected result

Each main-array element is a row containing a name and an age.

05 · PRACTICAL EXAMPLE

Return value and array reference

User Function ExAAddReferencia()
    Local aDestino := {}
    Local aLinha   := {"Produto", 10}
    Local xRetorno := AAdd(aDestino, aLinha)

    aLinha[2] := 20

    ConOut(xRetorno[2])    // 20
    ConOut(aDestino[1][2]) // 20
Return
Expected result

The return value is the appended row, and later source-array changes are visible through the stored reference.

BEST PRACTICES
  • Use Len() to read the new size.
  • An appended array is stored by reference.
  • Use AClone() when an independent nested copy is required.
  • Use AIns() for an existing position and ASize() for direct resizing.
  • AdvPL arrays are one-based.
COMMON PITFALLS
  • Do not assign aDestination := AAdd(...), because the function returns the appended value.
  • Keep matrix rows structurally consistent.
  • Avoid unbounded growth in loops.
  • Remember that the received array is modified.

Related content

REFERENCES
  1. TOTVS. AAdd. TDN.
  2. Terminal de Informação. aAdd. Community vector and matrix examples, originally published Nov. 13, 2016.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR