Sign in to follow this  
Followers 0
rudi

howto add rows to an array?

9 posts in this topic

Hi.

I'd like to add rows to a two dimensional array. Simplified to a 2 x 2 array this is what I tried:

#include <array.au3>

Dim $DummyArr[1][2]
Dim $myArr[2][2]


FillArr()
_ArrayDisplay($myArr)
_ArrayAdd($myArr, "SomeValue")
_ArrayDisplay($myArr, "Fresh spoiled ;)")

ReDim $myArr[2][2]
FillArr()
_ArrayDisplay($myArr, "Array initialized a 2nd time...")
; trying to use arrayadd() with a one row array, same number of collumns..

$DummyArr[0][0] = "Dummy 0, 0"
$DummyArr[0][1] = "Dummy 0, 1"
_ArrayDisplay($DummyArr, "Temporary Array to be added:")
_ArrayAdd($myArr, $DummyArr)
_ArrayDisplay($myArr, "Array spoiled again")

Func FillArr()
    $myArr[0][0] = "R0, C0"
    $myArr[1][0] = "R1, C0"
    $myArr[0][1] = "R0, C1"
    $myArr[1][1] = "R1, C1"
EndFunc   ;==>FillArr

Howto add rows in one step to an existing array?

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites



Not sure what you mean but maybe this?

#include <array.au3>
Dim $a1[3][2]=[[1,2],[43,65],[123,12389]] ; First array
Dim $a2[2][2]=[[1,2],[2,4]] ; This array will be added to the end of the first array
_ArrayCombine($a1,$a2)
_ArrayDisplay($a1)


Func _ArrayCombine(ByRef $array1,ByRef $array2) ; Function to combine 2D arrays
    For $i=0 To Ubound($array2,1)-1
        ReDim $array1[Ubound($array1,1)+1][2]
        $array1[Ubound($array1,1)-1][0]=$array2[$i][0]
        $array1[Ubound($array1,1)-1][1]=$array2[$i][1]
    Next
EndFunc

:)


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

Not sure what you mean but maybe this?

_ArrayCombine()

Hm, I can't find a single word covering _ArrayCombine() (3.2.10.0), also your code doesn't show syntax highlighting in SciTE, but it's running finde :) I'll have a look to 3.2.12.0 soon...

So this function isn't covered in the (old) help file?

Thanks, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Hm, I can't find a single word covering _ArrayCombine() (3.2.10.0), also your code doesn't show syntax highlighting in SciTE, but it's running finde :) I'll have a look to 3.2.12.0 soon...

So this function isn't covered in the (old) help file?

Thanks, Rudi.

Hahaha, I just invented "_ArrayCombine" :(


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi.

I'd like to add rows to a two dimensional array. Simplified to a 2 x 2 array this is what I tried:

Howto add rows in one step to an existing array?

Regards, Rudi.

_ArrayAdd() only adds a single row, and only to a 1D array. I have a 2D version, but it still adds a single row. This will take either 1D or 2D for $avArray, and if you pass it a single value, or an array with $NestArray = True, then it gets stored as a single element at [n][0]. If $avArray is 2D and you pass a 1D array with $NestArray = False, then it adds one full row to the array with the elements from the 1D array used for each column.

It is still one row at a time. To append a 2D array onto a 2D array (multiple rows), you need a 2D version of _ArrayConcatenate(), not _ArrayAdd().

This is what I have for 2D add (including full demo), __ArrayAdd():

#include <Array.au3>

Global $avTesting, $avAdd, $RET, $iErrSav, $iExtSav, $avTemp

; ----------------------------------------------
; Demonstrate normal operation
; ----------------------------------------------
$sTitle = "Demonstrate normal operation"

; Add an element to a 1D array:
$sMsg = "Add an element to a 1D array:" & @CRLF
Global $avTesting[3] = [1, 2, 3]
$RET = __ArrayAdd($avTesting, "Four")
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; Add a single element to a 2D array
$sMsg = "Add a single element to a 2D array:" & @CRLF
Global $avTesting[3][2] = [[1, 2],[3, 4],[5, 6]]
$RET = __ArrayAdd($avTesting, "Test")
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; Add a row of elements to a 2D array
$sMsg = "Add a row of elements to a 2D array:" & @CRLF
Global $avTesting[3][2] = [[1, 2],[3, 4],[5, 6]]
Global $avAdd[2] = ["x", "y"]
$RET = __ArrayAdd($avTesting, $avAdd, False)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; Same as above, but nest array as an element
$sMsg = "Same as above, but nest array as an element:" & @CRLF
Global $avTesting[3][2] = [[1, 2],[3, 4],[5, 6]]
Global $avAdd[2] = ["x", "y"]
$RET = __ArrayAdd($avTesting, $avAdd)
$iErrSav = @error
$iExtSav = @extended
$avTemp = $avTesting[$RET][0]
$sMsg &= "$RET = " & $RET & "  @error = " & $iErrSav & "  @extended = " & $iExtSav
SplashTextOn($sTitle, $sMsg, 400, 100, @DesktopWidth - 500, 100, 16)
_ArrayDisplay($avTemp, "$avTemp")

; ----------------------------------------------
; Demonstrate error handling
; ----------------------------------------------
$sTitle = "Demonstrate error handling"

; $avArray is not an array - @extended = 0
$sMsg = "$avArray is not an array:" & @CRLF
Global $avTesting = 0
$RET = __ArrayAdd($avTesting, "Test")
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; $avArray is greater than 2D - @extended = 1
$sMsg = "$avArray is greater than 2D:" & @CRLF
Global $avTesting[2][2][2] = [[[1, 2],[3, 4]],[[5, 6],[7, 8]]]
$RET = __ArrayAdd($avTesting, "Test")
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; $vValue as an array is not 1D - @extended = 2
$sMsg = "$vValue as an array is not 1D:" & @CRLF
Global $avTesting[3][2] = [[1, 2],[3, 4],[5, 6]]
Global $avAdd[2][2] = [["w", "x"],["y", "z"]]
$RET = __ArrayAdd($avTesting, $avAdd, False)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; $vValue as an array is too long - @extended = 3
$sMsg = "$vValue as an array is too long:" & @CRLF
Global $avTesting[3][2] = [[1, 2],[3, 4],[5, 6]]
Global $avAdd[3] = ["w", "x", "y"]
$RET = __ArrayAdd($avTesting, $avAdd, False)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

Func _DisplyResults()
    $sMsg &= "$RET = " & $RET & "  @error = " & $iErrSav & "  @extended = " & $iExtSav
    SplashTextOn($sTitle, $sMsg, 500, 100, @DesktopWidth - 600, 100, 16)
    If IsArray($avTesting) And UBound($avTesting, 0) < 3 Then
        _ArrayDisplay($avTesting, "$avTesting")
    Else
        MsgBox(64, "Pause", "Pause")
    EndIf
EndFunc ;==>_DisplyResults


; #FUNCTION# ====================================================================================================


================
; Name...........: __ArrayAdd
; Description ...: Adds a specified value or row at the end of an existing 1D or 2D array.
; Syntax.........: __ArrayAdd(ByRef $avArray, $vValue [, $NestArray = True])
; Parameters ....: $avArray - Array to modify ByRef
;                 $vValue  - Value to add, can be a 1D array if $avArray = 2D and $NestArray = False
;                 $NestArray - Optional flag if False causes array passed as $vValue to be interpreted as a 1D array
;                              of values to place in a single new row of a 2D array.  Default = True, saves array as
;                              a single element.  This flag is ignored if $avArray is 1D or $vValue is not an array.
; Return values .: Success - Index of last added item
;                 Failure - -1, sets @error to 1, and sets @extended to specify failure (see code below)
; Author ........: Jos van der Zande <jdeb at autoitscript dot com>
; Modified.......: Ultima - code cleanup
;             ; PsaltyDS - Array and 2D $vValue inputs added
; Remarks .......: Each call to this function adds exactly one new row to $avArray.  To add more rows use _ArrayConcatenate.
; Related .......: _ArrayConcatenate, _ArrayDelete, _ArrayInsert, _ArrayPop, _ArrayPush
; Link ..........;
; Example .......; Yes
; ====================================================================================================


===========================
Func __ArrayAdd(ByRef $avArray, $vValue, $NestArray = True)
    Local $iBoundArray0, $iBoundArray1, $iBoundArray2, $iBoundValue1
    
    If IsArray($avArray) = 0 Then Return SetError(1, 0, -1); $avArray is not an array
    
    $iBoundArray0 = UBound($avArray, 0); No. of dimesions in array
    If $iBoundArray0 > 2 Then Return SetError(1, 1, -1); $avArray is more than 2D
    
    $iBoundArray1 = UBound($avArray, 1); Size of array in first dimension
    If $iBoundArray0 = 2 Then $iBoundArray2 = UBound($avArray, 2); Size of array in second dimension
    
    If ($iBoundArray0 = 1) Or (IsArray($vValue) = 0) Or $NestArray Then
; If input array is 1D, or $vValue is not an array, or $NestArray = True (default) then save $vValue literally
        If $iBoundArray0 = 1 Then
    ; Add to 1D array
            ReDim $avArray[$iBoundArray1 + 1]
            $avArray[$iBoundArray1] = $vValue
        Else
    ; Add to 2D array at [n][0]
            ReDim $avArray[$iBoundArray1 + 1][$iBoundArray2]
            $avArray[$iBoundArray1][0] = $vValue
        EndIf
    Else
; If input array is 2D, and $vValue is an array, and $NestArray = False,
;   then $vValue is a 1D array of values to add as a new row.
        If UBound($vValue, 0) <> 1 Then Return SetError(1, 2, -1); $vValue array is not 1D
        $iBoundValue1 = UBound($vValue, 1)
        If $iBoundArray2 < $iBoundValue1 Then Return SetError(1, 3, -1); $vValue array has too many elements
        ReDim $avArray[$iBoundArray1 + 1][$iBoundArray2]
        For $n = 0 To $iBoundValue1 - 1
            $avArray[$iBoundArray1][$n] = $vValue[$n]
        Next
    EndIf
    
; Return index of new last row in $avArray
    Return $iBoundArray1
EndFunc ;==>__ArrayAdd

:)

Edited by PsaltyDS
1 person likes this

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Here is the matching 1D/2D capable __ArrayConcatenate() (includeing demo script) to go with __ArrayAdd() above:

#include <Array.au3>

Global $avTesting, $avAdd, $RET, $iErrSav, $iExtSav, $avTemp

; ----------------------------------------------
; Demonstrate normal operation
; ----------------------------------------------
$sTitle = "Demonstrate normal operation"

; Concatenate 1D arrays
$sMsg = "Concatenate 1D arrays:" & @CRLF
Global $avTesting[3] = [1, 2, 3]
Global $avAdd[3] = ["x", "y", "z"]
$RET = __ArrayConcatenate($avTesting, $avAdd)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; Concatenate 2D arrays
$sMsg = "Concatenate 2D arrays:" & @CRLF
Global $avTesting[3][3] = [[1, 2, 3],[4,5,6],[7,8,9]]
Global $avAdd[2][2] = [["x0","x1"],["y0","y1"]]
$RET = __ArrayConcatenate($avTesting, $avAdd)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; ----------------------------------------------
; Demonstrate error handling
; ----------------------------------------------
$sTitle = "Demonstrate error handling"

; 1 - $avArrayTarget is not an array
$sMsg = "1 - $avArrayTarget is not an array:" & @CRLF
Global $avTesting = 0
Global $avAdd[3] = ["x", "y", "z"]
$RET = __ArrayConcatenate($avTesting, $avAdd)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; 2 - $avArraySource is not an array
$sMsg = "2 - $avArraySource is not an array:" & @CRLF
Global $avTesting[3] = [1, 2, 3]
Global $avAdd = 0
$RET = __ArrayConcatenate($avTesting, $avAdd)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; 3 - 1D/2D dimensionality did not match
$sMsg = "3 - 1D/2D dimensionality did not match:" & @CRLF
Global $avTesting[3][3] = [[1, 2, 3],[4,5,6],[7,8,9]]
Global $avAdd[3] = ["x", "y", "z"]
$RET = __ArrayConcatenate($avTesting, $avAdd)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; 4 - At least one array was 3D or more
$sMsg = "4 - At least one array was 3D or more:" & @CRLF
Global $avTesting[2][2][2] = [[[0,1],[0,1]],[[0,1],[0,1]]]
Global $avAdd[2][2][2] = [[["x","y"],["x","y"]],[["x","y"],["x","y"]]]
$RET = __ArrayConcatenate($avTesting, $avAdd)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; 5 - 2D boundry of source too large for target 
$sMsg = "5 - 2D boundry of source too large for target :" & @CRLF
Global $avTesting[3][3] = [[1, 2, 3],[4,5,6],[7,8,9]]
Global $avAdd[2][4] = [["w","x","y","z"],["w","x","y","z"]]
$RET = __ArrayConcatenate($avTesting, $avAdd)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()


Func _DisplyResults()
    $sMsg &= "$RET = " & $RET & "  @error = " & $iErrSav & "  @extended = " & $iExtSav
    SplashTextOn($sTitle, $sMsg, 500, 100, @DesktopWidth - 600, 100, 16)
    If IsArray($avTesting) And UBound($avTesting, 0) < 3 Then
        _ArrayDisplay($avTesting, "$avTesting")
    Else
        MsgBox(64, "Pause", "Pause")
    EndIf
EndFunc;==>_DisplyResults


; #FUNCTION# =====================================================================
; Name...........: __ArrayConcatenate
; Description ...: Concatenate two 1D or 2D arrays
; Syntax.........: __ArrayConcatenate(ByRef $avArrayTarget, Const ByRef $avArraySource)
; Parameters ....: $avArrayTarget - The array to concatenate onto
;                 $avArraySource - The array to concatenate from - Must be 1D or 2D to match $avArrayTarget,
;                                  and if 2D, then Ubound($avArraySource, 2) <= Ubound($avArrayTarget, 2).
; Return values .: Success - Index of last added item
;                 Failure - -1, sets @error to 1 and @extended per failure (see code below)
; Author ........: Ultima
; Modified.......: PsaltyDS - 1D/2D version, changed return value and @error/@extended to be consistent with __ArrayAdd()
; Remarks .......:
; Related .......: __ArrayAdd, _ArrayPush
; Link ..........;
; Example .......; Yes
; ===============================================================================
Func __ArrayConcatenate(ByRef $avArrayTarget, Const ByRef $avArraySource)
    If Not IsArray($avArrayTarget) Then Return SetError(1, 1, -1); $avArrayTarget is not an array
    If Not IsArray($avArraySource) Then Return SetError(1, 2, -1); $avArraySource is not an array
    
    Local $iUBoundTarget0 = UBound($avArrayTarget, 0), $iUBoundSource0 = UBound($avArraySource, 0)
    If $iUBoundTarget0 <> $iUBoundSource0 Then Return SetError(1, 3, -1); 1D/2D dimensionality did not match
    If $iUBoundTarget0 > 2 Then Return SetError(1, 4, -1); At least one array was 3D or more
    
    Local $iUBoundTarget1 = UBound($avArrayTarget, 1), $iUBoundSource1 = UBound($avArraySource, 1)
    
    Local $iNewSize = $iUBoundTarget1 + $iUBoundSource1
    If $iUBoundTarget0 = 1 Then
        ; 1D arrays
        ReDim $avArrayTarget[$iNewSize]
        For $i = 0 To $iUBoundSource1 - 1
            $avArrayTarget[$iUBoundTarget1 + $i] = $avArraySource[$i]
        Next
    Else
        ; 2D arrays
        Local $iUBoundTarget2 = UBound($avArrayTarget, 2), $iUBoundSource2 = UBound($avArraySource, 2)
        If $iUBoundSource2 > $iUBoundTarget2 Then Return SetError(1, 5, -1); 2D boundry of source too large for target
        ReDim $avArrayTarget[$iNewSize][$iUBoundTarget2]
        For $r = 0 To $iUBoundSource1 - 1
            For $c = 0 To $iUBoundSource2 - 1
                $avArrayTarget[$iUBoundTarget1 + $r][$c] = $avArraySource[$r][$c]
            Next
        Next
    EndIf
    
    Return $iNewSize - 1
EndFunc;==>__ArrayConcatenate

:)

Edit: Note that with __ArrayAdd() I wanted strict backwards compatibility with the included _ArrayAdd() from Array.au3. The included _ArrayConcatenate() had a completely different Return/@error scheme, and I changed this one to be consistent with __ArrayAdd().

Edit2: Cleaned up line wrap for readability.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

Func _ArrayCombine(ByRef $array1,ByRef $array2) ; Function to combine 2D arrays in column direction
    Local $a1 = UBound ($array1,2)
    Local $a2 = Ubound($array2,1)
    ReDim $array1[UBound ($array1,1)][UBound ($array1,2)+UBound ($array2,2)]
    For $i=0 To $a2 -1
            For $x= $a1 to $a1+UBound ($array2,2)-1
                $y= $x - $a1
                $array1[$i][$x]=$array2[$i][$y]
            Next
    Next
EndFunc

Not sure if this was in there but it should add rows/columns in the column direction rather than the row direction.


I suck at programming... But I try really hard! :imwithstupid:

Share this post


Link to post
Share on other sites

_ArrayAdd() only adds a single row, and only to a 1D array. I have a 2D version, but it still adds a single row. This will take either 1D or 2D for $avArray, and if you pass it a single value, or an array with $NestArray = True, then it gets stored as a single element at [n][0]. If $avArray is 2D and you pass a 1D array with $NestArray = False, then it adds one full row to the array with the elements from the 1D array used for each column.

It is still one row at a time. To append a 2D array onto a 2D array (multiple rows), you need a 2D version of _ArrayConcatenate(), not _ArrayAdd().

This is what I have for 2D add (including full demo), __ArrayAdd():

#include <Array.au3>

Global $avTesting, $avAdd, $RET, $iErrSav, $iExtSav, $avTemp

; ----------------------------------------------
; Demonstrate normal operation
; ----------------------------------------------
$sTitle = "Demonstrate normal operation"

; Add an element to a 1D array:
$sMsg = "Add an element to a 1D array:" & @CRLF
Global $avTesting[3] = [1, 2, 3]
$RET = __ArrayAdd($avTesting, "Four")
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; Add a single element to a 2D array
$sMsg = "Add a single element to a 2D array:" & @CRLF
Global $avTesting[3][2] = [[1, 2],[3, 4],[5, 6]]
$RET = __ArrayAdd($avTesting, "Test")
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; Add a row of elements to a 2D array
$sMsg = "Add a row of elements to a 2D array:" & @CRLF
Global $avTesting[3][2] = [[1, 2],[3, 4],[5, 6]]
Global $avAdd[2] = ["x", "y"]
$RET = __ArrayAdd($avTesting, $avAdd, False)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; Same as above, but nest array as an element
$sMsg = "Same as above, but nest array as an element:" & @CRLF
Global $avTesting[3][2] = [[1, 2],[3, 4],[5, 6]]
Global $avAdd[2] = ["x", "y"]
$RET = __ArrayAdd($avTesting, $avAdd)
$iErrSav = @error
$iExtSav = @extended
$avTemp = $avTesting[$RET][0]
$sMsg &= "$RET = " & $RET & "  @error = " & $iErrSav & "  @extended = " & $iExtSav
SplashTextOn($sTitle, $sMsg, 400, 100, @DesktopWidth - 500, 100, 16)
_ArrayDisplay($avTemp, "$avTemp")

; ----------------------------------------------
; Demonstrate error handling
; ----------------------------------------------
$sTitle = "Demonstrate error handling"

; $avArray is not an array - @extended = 0
$sMsg = "$avArray is not an array:" & @CRLF
Global $avTesting = 0
$RET = __ArrayAdd($avTesting, "Test")
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; $avArray is greater than 2D - @extended = 1
$sMsg = "$avArray is greater than 2D:" & @CRLF
Global $avTesting[2][2][2] = [[[1, 2],[3, 4]],[[5, 6],[7, 8]]]
$RET = __ArrayAdd($avTesting, "Test")
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; $vValue as an array is not 1D - @extended = 2
$sMsg = "$vValue as an array is not 1D:" & @CRLF
Global $avTesting[3][2] = [[1, 2],[3, 4],[5, 6]]
Global $avAdd[2][2] = [["w", "x"],["y", "z"]]
$RET = __ArrayAdd($avTesting, $avAdd, False)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

; $vValue as an array is too long - @extended = 3
$sMsg = "$vValue as an array is too long:" & @CRLF
Global $avTesting[3][2] = [[1, 2],[3, 4],[5, 6]]
Global $avAdd[3] = ["w", "x", "y"]
$RET = __ArrayAdd($avTesting, $avAdd, False)
$iErrSav = @error
$iExtSav = @extended
_DisplyResults()

Func _DisplyResults()
    $sMsg &= "$RET = " & $RET & "  @error = " & $iErrSav & "  @extended = " & $iExtSav
    SplashTextOn($sTitle, $sMsg, 500, 100, @DesktopWidth - 600, 100, 16)
    If IsArray($avTesting) And UBound($avTesting, 0) < 3 Then
        _ArrayDisplay($avTesting, "$avTesting")
    Else
        MsgBox(64, "Pause", "Pause")
    EndIf
EndFunc ;==>_DisplyResults


; #FUNCTION# ====================================================================================================


================
; Name...........: __ArrayAdd
; Description ...: Adds a specified value or row at the end of an existing 1D or 2D array.
; Syntax.........: __ArrayAdd(ByRef $avArray, $vValue [, $NestArray = True])
; Parameters ....: $avArray - Array to modify ByRef
;                 $vValue  - Value to add, can be a 1D array if $avArray = 2D and $NestArray = False
;                 $NestArray - Optional flag if False causes array passed as $vValue to be interpreted as a 1D array
;                              of values to place in a single new row of a 2D array.  Default = True, saves array as
;                              a single element.  This flag is ignored if $avArray is 1D or $vValue is not an array.
; Return values .: Success - Index of last added item
;                 Failure - -1, sets @error to 1, and sets @extended to specify failure (see code below)
; Author ........: Jos van der Zande <jdeb at autoitscript dot com>
; Modified.......: Ultima - code cleanup
;             ; PsaltyDS - Array and 2D $vValue inputs added
; Remarks .......: Each call to this function adds exactly one new row to $avArray.  To add more rows use _ArrayConcatenate.
; Related .......: _ArrayConcatenate, _ArrayDelete, _ArrayInsert, _ArrayPop, _ArrayPush
; Link ..........;
; Example .......; Yes
; ====================================================================================================


===========================
Func __ArrayAdd(ByRef $avArray, $vValue, $NestArray = True)
    Local $iBoundArray0, $iBoundArray1, $iBoundArray2, $iBoundValue1
    
    If IsArray($avArray) = 0 Then Return SetError(1, 0, -1); $avArray is not an array
    
    $iBoundArray0 = UBound($avArray, 0); No. of dimesions in array
    If $iBoundArray0 > 2 Then Return SetError(1, 1, -1); $avArray is more than 2D
    
    $iBoundArray1 = UBound($avArray, 1); Size of array in first dimension
    If $iBoundArray0 = 2 Then $iBoundArray2 = UBound($avArray, 2); Size of array in second dimension
    
    If ($iBoundArray0 = 1) Or (IsArray($vValue) = 0) Or $NestArray Then
; If input array is 1D, or $vValue is not an array, or $NestArray = True (default) then save $vValue literally
        If $iBoundArray0 = 1 Then
    ; Add to 1D array
            ReDim $avArray[$iBoundArray1 + 1]
            $avArray[$iBoundArray1] = $vValue
        Else
    ; Add to 2D array at [n][0]
            ReDim $avArray[$iBoundArray1 + 1][$iBoundArray2]
            $avArray[$iBoundArray1][0] = $vValue
        EndIf
    Else
; If input array is 2D, and $vValue is an array, and $NestArray = False,
;   then $vValue is a 1D array of values to add as a new row.
        If UBound($vValue, 0) <> 1 Then Return SetError(1, 2, -1); $vValue array is not 1D
        $iBoundValue1 = UBound($vValue, 1)
        If $iBoundArray2 < $iBoundValue1 Then Return SetError(1, 3, -1); $vValue array has too many elements
        ReDim $avArray[$iBoundArray1 + 1][$iBoundArray2]
        For $n = 0 To $iBoundValue1 - 1
            $avArray[$iBoundArray1][$n] = $vValue[$n]
        Next
    EndIf
    
; Return index of new last row in $avArray
    Return $iBoundArray1
EndFunc ;==>__ArrayAdd

:)

 

PsaltyDS - wanted to take some time to thank you for your contributions - helped me shave down a few days to turn around a project.

Share this post


Link to post
Share on other sites

PsaltyDS - wanted to take some time to thank you for your contributions - helped me shave down a few days to turn around a project.

 

That post was 6 years ago, a simple "Like This" button click would've done it, no need to resurrect an ancient topic for a thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0