Jump to content

1D to 2D Array


jugador
 Share

Recommended Posts

> 1D to 2D Array

#include<array.au3>

Local  $o_Eg1[] = ['A','1','111','B','2','222','C','3','333','D','4' ]
_ArrayDisplay( __1Dto2DArray($o_Eg1, 3) )

Local  $o_Eg2[] = ['A','1','B','2','C','3','D','4','E','5' ]
_ArrayDisplay( __1Dto2DArray($o_Eg2, 2) )

; #FUNCTION# =============================================================================
; Name...........: __1Dto2DArray
; =========================================================================================
Func __1Dto2DArray($0_Array, $0_mod = 2)
    If UBound($0_Array, 2) > 1 Then Return SetError(1)
    Local  $o_Array[Ceiling(UBound($0_Array) / $0_mod)][$0_mod]
        For $i = 0 To UBound($0_Array) - 1
            $o_Array[Floor($i / $0_mod)][Mod($i, $0_mod)] = $0_Array[$i]
        Next
    Return $o_Array
EndFunc

 

Edited by jugador
Link to comment
Share on other sites

> 2D to 1D Array

#include <array.au3>

Local $fillArray_B[6][] = [['A', 1], ['B', 2], ['C', 3], ['D', 4], ['E', 5], ['F', 6]]
_ArrayDisplay($fillArray_B)

_ArrayDisplay( __2Dto1DArray($fillArray_B) )
_ArrayDisplay( __2Dto1DArray($fillArray_B, False) )

Local $fillArray_C[6][] = [['A', 1, 'AAA'], ['B', 2, 'BBB'], ['C', 3, 'CCC'], ['D', 4, 'DDD'], ['E', 5, 'EEE'], ['F', 6, 'FFF']]
_ArrayDisplay($fillArray_C)

_ArrayDisplay( __2Dto1DArray($fillArray_C) )
_ArrayDisplay( __2Dto1DArray($fillArray_C, False) )

; #FUNCTION# =============================================================================
; Name...........: __2Dto1DArray
; =========================================================================================
Func __2Dto1DArray($0_Array, $0_byRow = True)
    If UBound($0_Array, 2) < 1 Then Return SetError(1)
    Local $o_1DArray[UBound($0_Array) * UBound($0_Array, 2)]

    Local $o_Flag = ($0_byRow ? UBound($0_Array) : UBound($0_Array, 2))
    For $i = 0 To UBound($0_Array) - 1
        For $k = 0 To UBound($0_Array, 2) - 1
            If $0_byRow Then
                $o_1DArray[($o_Flag * $k) + $i] = $0_Array[$i][$k]
            Else
                $o_1DArray[($o_Flag * $i) + $k] = $0_Array[$i][$k]
            Endif
        Next
    Next
    Return $o_1DArray
EndFunc

As my coding knowledge limit to basic stuff :yawn:  so its end over here.
Now back to asking mode :D .

Edited by jugador
Link to comment
Share on other sites

  • jugador changed the title to 1D to 2D Array

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