Jump to content

Add Dimension to Established Array


strate
 Share

Recommended Posts

I need to add dimensions to a already establised array. When searching I found the following by PsaltyDS.

#include <Array.au3>

Global $avArray[1] = [0]
ConsoleWrite(UBound($avArray,0) & @crlf)
For $e = 1 To 10
    $iRow = UBound($avArray)
    $iCol = UBound($avArray, 2)
    ReDim $avArray[$iRow + 1][$iCol + 1]
    For $n = 0 To $iCol
        $avArray[$iRow][$n] = $e ^ $n
    Next
    $avArray[0][0] = UBound($avArray) - 1
    _ArrayDisplay($avArray, "$avArray")
Next
ConsoleWrite(UBound($avArray,0) & @crlf)

Problem with this is when you wrap it with "ConsoleWrite(UBound($avArray,0) & @crlf)" it doesn't appear to actually add dimensions, it returns 2 instead of 10.

So as stated previously, I need to add a dimension to a establised array. Is this possible?

INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...
Link to comment
Share on other sites

That's because Ubound($arr,0) returns the number of dimensions, not the number of elements in a dimension.

See the helpfile for UBound()

ConsoleWrite(UBound($avArray,1) & @crlf)
ConsoleWrite(UBound($avArray,2) & @crlf)
Edited by spudw2k
Link to comment
Share on other sites

Thats exactly the reason I used it (test for dimension creation). I can redim a dimension to add more records to it all day. My problem is if I have a 3D array how do I make it a 4D array.

INI TreeViewA bus station is where a bus stops, a train station is where a train stops. Onmy desk I have a work station...
Link to comment
Share on other sites

You should be able to ReDim any array to increase or decrease the dimensions. How you implement it is another thing.

Dim $array[10][10][10] creates a 3d array with 10 elements in each dimension

ReDim $array[10][10][10][10] adds a 4th dimension (with 10 elements) and preserves contents of first 3 dimensions

ReDim $array[1][1][1][1] Removes all but first elements of all for dimensions.

Edited by spudw2k
Link to comment
Share on other sites

Right, but how would you do that dynamically through the code such as the example in post #1 emulates?

Well his simply increases the size (elements) of the dimensions, not the number of dimensions. Good question though. Investigating.

It doesn't appear that AutoIt is capable (with it's pre-built functions) of dynamically creating multi-dimension arrays.

Edited by spudw2k
Link to comment
Share on other sites

Well his simply increases the size (elements) of the dimensions, not the number of dimensions. Good question though. Investigating.

It doesn't appear that AutoIt is capable (with it's pre-built functions) of dynamically creating multi-dimension arrays.

AutoIt can create arrays with up to 64 indexes (dimensions), and ReDim can change the number of indexes up or down within that range, but all data in the elements will be lost.

If you ReDim an array to a different index, but still with the same number of indexes, then data is not lost (well, except for trimmed-off elements when the new index is smaller).

The only way to add or remove indexes (dimensions) without data loss is to create the array with the target number of indexes and then copy data from the old array to the new.

This demo creates a [3][3][3] array and populates it with some text, then adds a fourth index (now [3][3][3][3]). Finally, it ReDim's the array without changing the number of indexes to show that all data is not lost:

; Declare a 3D array
Global $avArray[3][3][3] 

; Populate 3D array
For $x = 0 To UBound($avArray) - 1
    For $y = 0 To UBound($avArray, 2) - 1
        For $z = 0 To UBound($avArray, 3) - 1
            $avArray[$x][$y][$z] = "X" & $x & "_Y" & $y & "_Z" & $z
        Next
    Next
Next
ConsoleWrite("Original 3D [3][3][3] array:" & @LF)
_ArrayDisplay3D4D($avArray)

; Create temp 4D array and populate from 3D array
Global $avTemp[3][UBound($avArray)][UBound($avArray, 2)][UBound($avArray, 3)]
For $t = 0 To UBound($avTemp) - 1
    For $x = 0 To UBound($avTemp, 2) - 1
        For $y = 0 To UBound($avTemp, 3) - 1
            For $z = 0 To UBound($avTemp, 4) - 1
                $avTemp[$t][$x][$y][$z] = "T" & $t & "_X" & $x * ($t + 1) & "_Y" & $y * ($t + 1) & "_Z" & $z * ($t + 1)
            Next
        Next
    Next
Next

; Copy back to original array
$avArray = $avTemp
ConsoleWrite(@LF & "Now 4D [3][3][3][3] array:" & @LF)
_ArrayDisplay3D4D($avArray)

; Reduce the size of the dimensions
ReDim $avArray[2][2][2][2]
ConsoleWrite(@LF & "Now 4D [2][2][2][2] array:" & @LF)
_ArrayDisplay3D4D($avArray)


Func _ArrayDisplay3D4D(ByRef $avInput)
    $iDims = UBound($avInput, 0)
    For $a = 0 to UBound($avInput) - 1
        For $b = 0 to UBound($avInput, 2) - 1
            For $c = 0 to UBound($avInput, 3) - 1
                If UBound($avInput, 0 ) = 4 Then
                    For $d = 0 to UBound($avInput, 4) - 1
                        ConsoleWrite("[" & $a & "][" & $b & "][" & $c & "][" & $d & "] = " & $avInput[$a][$b][$c][$d] & @LF)
                    Next
                Else
                    ConsoleWrite("[" & $a & "][" & $b & "][" & $c & "] = " & $avInput[$a][$b][$c] & @LF)
                EndIf
            Next
        Next
    Next
EndFunc

:mellow:

Edit: Added ReDim to the the demo.

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...