Sign in to follow this  
Followers 0
dxt

referencing a multidimensional array index

5 posts in this topic

I have an array like

Global $a[2][2] = [[1,2],[3,4]]

and I want to pass the first element [1,2] to a function

I tried

f($a[0])

Func f(ByRef $ar)

ConsoleWrite($ar[0] & @CR)

EndFunc

and it complains that $a[0] has incorrect number of subscripts. So obviously you can't do this. Is there a way to accomplish this? It is the same as trying $b = $a[0];

Share this post


Link to post
Share on other sites



Something like this?:

Global $a[2][2] = [[1,2],[3,4]]

f($a)

Func f(ByRef $ar)
    For $i=0 to UBound($ar)-1
        ConsoleWrite($ar[$i][0]&$ar[$i][1]&@CRLF)
    Next
EndFunc

Share this post


Link to post
Share on other sites

The first element is $a[0][0] and it contains 1. The last element is [1][1] and it contains 4.

You must provide a value for every index on every reference (two in this case).

You can't reference the entire single row with $a[0]

One way to display it might be:

Global $aArray[2][2] = [[1,2],[3,4]]
For $r = 0 To UBound($aArray) - 1
    For $c = 0 To UBound($aArray, 2) - 1
        ConsoleWrite("$aArray[" & $r & "][" & $c & "] = " & $aArray[$r][$c] & @LF)
    Next
Next

;)


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

Something like this?:

Global $a[2][2] = [[1,2],[3,4]]

f($a)

Func f(ByRef $ar)
    For $i=0 to UBound($ar)-1
        ConsoleWrite($ar[$i][0]&$ar[$i][1]&@CRLF)
    Next
EndFunc

Ok, so I guess you can't reference a row. I guess the equivalent, but more bulky, would be

f($a, 0)

Func f(ByRef $ar, $index)

etc ...

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

To add to the good advise already mentioned above: on some rare occasions it may make sense to store an entire row as a string. The string may need to be expanded, depending on what it is you need it for.

Local $as_Rows[2] = ["A,B", "C,D"] ; The 2D array represented as a one dimensional array of strings.
$as_SelectedRow = StringSplit($as_Rows[0], ",", 2) ; Select and split the first row. => $as_Rows[0]
MsgBox(0,"", "1st row - 1st element = " & $as_SelectedRow[0] & @CRLF & "1st row - 2nd element = " & $as_SelectedRow[1])

In most cases there is no benefit to doing this, however it can be a viable solution to certain types of problem.

Edited by czardas

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