Jump to content

Multi Dimensional array help


Recommended Posts

can't access to array inside multi dimensional array. what im doing wrong and how this work in Autoit?

local $arr[20][20][2] = [[[12,0],[2,0],[7,0],[8,0],[9,0],[5,1],[10,5],[9,6],[8,7],[7,8],[6,9],[6,10],[7,10],[8,10],[9,10],[10,10],[11,10]]]

;~ ConsoleWrite(Ubound($arr[0])) ; ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
ConsoleWrite($arr[0][0][0]&@CRLF)        ; 12. all ok

;~ wtf($arr[0])    ; ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

local $hmm[20][2] = [[12,0],[2,0],[7,0],[8,0],[9,0],[5,1],[10,5],[9,6],[8,7],[7,8],[6,9],[6,10],[7,10],[8,10],[9,10],[10,10],[11,10]]

wtf($hmm)        ; working well but $hmm and $arr[0] suppose to be the same

func wtf($ar)
   for $i=0 to 2
      ConsoleWrite($ar[$i][0]&@CRLF)
   Next
EndFunc

 

Link to post
Share on other sites
Global $arr[20][20][2] = [[[12, 0], [2, 0], [7, 0], [8, 0], [9, 0], [5, 1], [10, 5], [9, 6], [8, 7], [7, 8], [6, 9], [6, 10], [7, 10], [8, 10], [9, 10], [10, 10], [11, 10]]]
Global $hmm[20][2] = [[12, 0], [2, 0], [7, 0], [8, 0], [9, 0], [5, 1], [10, 5], [9, 6], [8, 7], [7, 8], [6, 9], [6, 10], [7, 10], [8, 10], [9, 10], [10, 10], [11, 10]]
wtf($arr)        ; working well but $hmm and $arr[0] suppose to be the same
wtf($hmm)        ; working well but $hmm and $arr[0] suppose to be the same
Func wtf($ar)
    If UBound($ar, 3) Then
        For $i = 0 To UBound($ar) - 1
            For $i2 = 0 To UBound($ar, 2) - 1
                If $ar[$i][$i2][0] = "" Then ContinueLoop
                For $i3 = 0 To UBound($ar, 3) - 1
                    ConsoleWrite($ar[$i][$i2][$i3] & @TAB)
                Next
                ConsoleWrite(@CRLF)
            Next
        Next
    Else
        For $i = 0 To UBound($ar) - 1
            If $ar[$i][0] = "" Then ContinueLoop
            For $i2 = 0 To UBound($ar, 2) - 1
                ConsoleWrite($ar[$i][$i2] & @TAB)
            Next
            ConsoleWrite(@CRLF)
        Next
    EndIf
    ConsoleWrite('> ======================================================================' & @CRLF)
EndFunc   ;==>wtf

so, what do you wanna do ?

Edited by argumentum
better code
Link to post
Share on other sites
8 hours ago, argumentum said:

so, what do you wanna do ?

thank you. i forget about Ubound() second parameter 😃

fortran and basic was in my life 20+ yrs ago. arr.length more usual for me )

thx.
 

Link to post
Share on other sites

is that possible in Autoit to pass array from multi dimensional array?

local $arr[20][20][2] = [[[1,2],[3,4]]]

wtf($arr[0])       ; ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
local $hmm = [[1,2],[3,4]]
wtf($hmm)       ;  working well but $hmm and $arr[0] suppose to be the same

func wtf($ar)    
EndFunc

 

Link to post
Share on other sites

no, but then again, what for. Depending on the language you're coding in you're gonna have liberties and limitations as far as the concepts incorporated in the language. So "think AutoIt", because if you "think Fortran", you're gonna have a hard time figuring out what's wrong with your AutoIt code.

PS: I like you avatar :D 

Edited by argumentum
dyslexia =)
Link to post
Share on other sites

Actually, you are passing a multi-dimensional array to the function. One of your problems is in one call of the wtf function, you are passing a 3d array and the next, you are passing a 2d array. 

To fix the problem of one function reading arrays with different amount of dimensions, I added a 2nd parameter for how many dimensions. Without something like this, you will always get an error that the array is out of bounds.

In both examples, I am reading the same array position with a msgbox, but from different arrays. The arrays are referenced in order of z,y,x axis. In the 2d array, there is no z, so only y and x are used. You can play with the numbers to get a better idea. Normally, under a 1d or 2d array, I use _arraydisplay for examples, but it does not display 3d arrays. I have wanted to create something to display 3d arrays for a while now, but I haven't had the time to do so. Maybe some day.

#include <Array.au3>

Example()

Func Example()
    Local $arr[20][20][2] = [[[12, 0], [2, 0], [7, "3d pos 0,2,1"], [8, 0], [9, 0], [5, 1], [10, 5], [9, 6], [8, 7], [7, 8], [6, 9], [6, 10], [7, 10], [8, 10], [9, 10], [10, 10], [11, 10]]]
    Local $hmm[20][2] = [[12, 0], [2, 0], [7, "2d pos 2,1"], [8, 0], [9, 0], [5, 1], [10, 5], [9, 6], [8, 7], [7, 8], [6, 9], [6, 10], [7, 10], [8, 10], [9, 10], [10, 10], [11, 10]]

    wtf($arr, 3)    ; passes $arr to function
    wtf($hmm, 2)    ; passes $hmm to function
EndFunc

Func wtf(ByRef $ar, $numDim)
    If $numDim = 3 Then
        MsgBox(0, "", $ar[0][2][1]) ; msgbox reads 3d array
    ElseIf $numDim = 2 Then
        MsgBox(0, "", $ar[2][1])    ; msgbox reads 2d array
    EndIf
EndFunc   ;==>wtf

 

Edited by abberration
changed global arrays to locals and put into example function
Link to post
Share on other sites

Improved: don't need to specify how many dimensions:

#include <Array.au3>

Example()

Func Example()
    Local $arr[20][20][2] = [[[12, 0], [2, 0], [7, "3d pos 0,2,1"], [8, 0], [9, 0], [5, 1], [10, 5], [9, 6], [8, 7], [7, 8], [6, 9], [6, 10], [7, 10], [8, 10], [9, 10], [10, 10], [11, 10]]]
    Local $hmm[20][2] = [[12, 0], [2, 0], [7, "2d pos 2,1"], [8, 0], [9, 0], [5, 1], [10, 5], [9, 6], [8, 7], [7, 8], [6, 9], [6, 10], [7, 10], [8, 10], [9, 10], [10, 10], [11, 10]]

    wtf($arr)   ; passes $arr to function
    wtf($hmm)   ; passes $hmm to function
EndFunc

Func wtf(ByRef $ar)
    If UBound($ar,0) = 3 Then
        MsgBox(0, "", $ar[0][2][1]) ; msgbox reads 3d array
    ElseIf UBound($ar,0) = 2 Then
        MsgBox(0, "", $ar[2][1])    ; msgbox reads 2d array
    EndIf
EndFunc   ;==>wtf

 

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...