Jump to content

referencing a multidimensional array index


dxt
 Share

Recommended Posts

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];

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

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
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...