Sign in to follow this  
Followers 0
sam71

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

4 posts in this topic

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

Share this post


Link to post
Share on other sites



This is the way to go:

Dim $array2d[10][10]
Dim $array1d[10]

For $i = 0 To UBound[$array2d]-1
    $array1d[$i] = $array2d[$i]
Next

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
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
;

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