Jump to content

multi-dimensional array to merged flat arrays.


MvGulik
 Share

Recommended Posts

Just something I got hooked on.

;; converts any multi-dimensional array into a array consisting of merged single dimentional arrays.
;; basic version, using variables only when realy needed.
;; basic error/extended pass-on.
;; note: the code "For $var[array] ..." generates false AU3Check error. (before v3.3.1.4)
Func ArrayGetCell1(Const ByRef $loop, Const ByRef $array)
    Local $data, $sExec = '$array'
    For $i = 1 To UBound($loop) - 1
        $sExec &= '[' & String($loop[$i]) & ']'
    Next
    $data = Execute($sExec)
    If IsArray($data) Then $data = arrayConvert1($data) ;; do internal array's.
    Return SetError(@error, @extended, $data)
EndFunc
Func ArrayRecurse1(ByRef $loop, Const ByRef $array)
    Local $newArray[UBound($array, $loop[0])]
    Local $data
    For $loop[$loop[0]] = 0 To UBound($array, $loop[0]) - 1
        If UBound($array, 0) > $loop[0] Then ;; level up.
            $loop[0] += 1
            $data = ArrayRecurse1($loop, $array)
            If @error Then Return SetError(@error, @extended)
            $loop[0] -= 1
        Else ;; data collect.
            $data = ArrayGetCell1($loop, $array)
            If @error Then Return SetError(@error, @extended)
        EndIf
        $newArray[$loop[$loop[0]]] = $data
    Next
    Return SetError(@error, @extended, $newArray)
EndFunc
Func arrayConvert1($array)
    If Not IsArray($array) Then Return SetError(91,0,0)
    Local $loop[UBound($array, 0) + 1] = [1] ;; [dim_level_tracker, dim1_tracker, .. ,dimX_tracker]
    Local $newArray = ArrayRecurse1($loop, $array) ;; none byref.
    Return SetError(@error, @extended, $newArray) ;; none byref.
;~  $array = ArrayRecurse1($loop, $array) ;; byref.
;~  Return SetError(@error, @extended, (@error=0)) ;; byref.
EndFunc

---

#4: Same approach to multi-dimensional array's rolled into one function.

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

only rational reason I can think of is multi-array support for something like a data converter. like JSON.

---

I consider it more as a template to deal with arrays of unknown dimensions.

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Hi,

I did something similar for displaying array (n-dimensional) contents in a debugger window. It converts to a string and is very compact, but then there is no error handling in the way you do.

Func _DispArr(ByRef $ar, ByRef $out, $d=0, $cnt=0)      ;display values of n-dimension array
    Local $res

    If $cnt = 0 Then Dim $cnt[UBound($ar, 0)]

    For $i = 0 to UBound($ar, $d+1)
        if $i = UBound($ar, $d+1) Then Return $d-1
        $cnt[$d] = $i
        if $d < UBound($ar, 0) - 1 Then $d = _DispArr($ar, $out, $d+1, $cnt)    ;recursive call
        if $d = UBound($ar, 0) - 1 Then
            $res = Execute("$ar[" & _ArrayToString($cnt,"][") & "]")
            $out &= @CRLF & StringTrimLeft($DBGident, 4) & "[" & _ArrayToString($cnt,"][") & "] " & _Type($res) & ' = ' & @TAB & _Value($res, true)
        EndIf
    Next
EndFunc

I think you have the same approach but I didn't study it. As usual I get headache wich these recursive things...

- Heron -

Link to comment
Share on other sites

Yes, its the same approach.

Yours is however a lot nicer to look at.

Thanks for sharing it.

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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