Jump to content

reading a 2d array line into a 1d array?


Recommended Posts

Quick and dirty.

Dim $two_d[3][2] = [[0,0],[1,1],[2,2]]
Dim $one_d[6]

For $i = 0 To 2
    $one_d[$i * 2] = $two_d[$i][0]
    $one_d[$i * 2 + 1] = $two_d[$i][1]
Next

Edit: Hey enaiman, your method doesn't make the data index accessible. I admit that it does fit the OP vaguely asked for though.

Edited by Richard Robertson
Link to comment
Share on other sites

I'm trying to read the line (index of a 2d array and place it into a 1d array)

Any luck someone can help me on this?

Thanks

Good luck with reading the line.

Here is a couple of interruptions of what you could be asking.

;
#include <Array.au3>

Local $aArray2d[4][3] = [[0, 0, 0],[1, 1, 1],[2, 2, 2],[3, 3, 3]]

; Index of 2D array in 1D array
Local $a1d[12] = [$aArray2d[0][0], $aArray2d[0][1], $aArray2d[0][2], _
                  $aArray2d[1][0], $aArray2d[1][1], $aArray2d[1][2], _
                  $aArray2d[2][0], $aArray2d[2][1], $aArray2d[2][2], _
                  $aArray2d[3][0], $aArray2d[3][1], $aArray2d[3][2]]

_ArrayDisplay($a1d, "Index of 2D array in 1D array")

;======================================================================================
;Copy the contents of 2D array of variables to the contents of a 1D array of variables.
Local $aArray1D = _Array2DTo1D($aArray2d)

_ArrayDisplay($aArray1D)


Func _Array2DTo1D($aTwo_d)
    If UBound($aTwo_d, 0) <> 2 Then
        MsgBox(0, "Error", "Array not 2 dimensional")
        Return
    EndIf
    Local $iRow = UBound($aTwo_d)
    Local $iCol = UBound($aTwo_d, 2)
    Local $aOne_d[$iRow * $iCol]

    For $i = 0 To $iRow - 1
        For $j = 0 To $iCol - 1
            $aOne_d[$i * $iCol + $j] = $aTwo_d[$i][$j]
        Next
    Next
    Return $aOne_d
EndFunc   ;==>_Array2DTo1D
;
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...