Jump to content

Array create function. (Any dimension)


MvGulik
 Share

Recommended Posts

(Recycled Topic)

Func _DimArray($iDims, $aiDims = '')
    If Not IsNumber($iDims) Then Return SetError(101)
    Local $aOut
    Switch $iDims
        Case 1
            Dim $aOut[1]
        Case 2
            Dim $aOut[1][1]
        Case 3
            Dim $aOut[1][1][1]
            ;; etc. -> Create_SwitchCase(1)
        Case 64
            Dim $aOut[1][1][1][1] ;; & 60x'[1]'
        Case Else
            Return SetError(103)
    EndSwitch
    If IsArray($aiDims) And UBound($aiDims, 1) = $iDims Then
        _Redim($aOut, $aiDims)
        If @error Then Return SetError(@error)
    ElseIf not IsString($aiDims) or $aiDims Then ;; flag ignored $aiDims error. (none fatal)
        SetError(-1)
    EndIf
    Return $aOut
EndFunc

Func _Redim(ByRef $aOut, $aiDims)
    If Not (IsArray($aOut) And IsArray($aiDims)) Then Return SetError(201) ;; wrong data types.
    If Not (UBound($aOut, 0) = UBound($aiDims, 1)) Then Return SetError(202) ;; enforeced dimention match.
    ;; data checkup.
    Local $iCels = 1
    For $e In $aiDims
        If Not IsInt($e) Or $e < 1 Then Return SetError(204) ;; not a int, zero, or negative.
        $iCels *= $e
    Next
    If $iCels > Int(2 ^ 24) Then Return SetError(205) ;; max array elements exceeded.
    Switch UBound($aOut, 0)
        Case 1
            ReDim $aOut[$aiDims[0]]
        Case 2
            ReDim $aOut[$aiDims[0]][$aiDims[1]]
        Case 3
            ReDim $aOut[$aiDims[0]][$aiDims[1]][$aiDims[2]]
            ;; etc. -> Create_SwitchCase(2)
        Case 64
            Dim $aOut[$aiDims[0]][$aiDims[1]][$aiDims[2]][$aiDims[3]] ;; & 60x'[$aiDims[iDim]]'
        Case Else
            Return SetError(203)
    EndSwitch
EndFunc

;~ Create_SwitchCase(1) ;; 1|2
Func Create_SwitchCase($id)
    If Not ($id = 1 Or $id = 2) Then Return SetError(96)

    Local Const $sNameA = '$aOut'
    Local Const $sNameI = '$iDims'
    Local Const $sNameD = '$aiDims'
    Local Const $sNL = @CRLF

    Local $sDump = ''
    Switch $id
        Case 1
            $sDump &= 'Switch ' & $sNameI & $sNL
        Case 2
            $sDump &= 'Switch UBound(' & $sNameA & ',0)' & $sNL
    EndSwitch
    For $i = 0 To 63
        $sDump &= 'Case ' & String($i + 1) & $sNL
        Switch $id
            Case 1
                $sDump &= 'Dim ' & $sNameA
                For $j = 0 To $i
                    $sDump &= '[1]'
                Next
            Case 2
                $sDump &= 'ReDim ' & $sNameA
                For $j = 0 To $i
                    $sDump &= '[' & $sNameD & '[' & String($j) & ']]'
                Next
        EndSwitch
        $sDump &= $sNL
    Next
    $sDump &= 'Case Else' & $sNL
    Switch $id
        Case 1
            $sDump &= 'Return SetError(103)' & $sNL
        Case 2
            $sDump &= 'Return SetError(203)' & $sNL
    EndSwitch
    $sDump &= 'EndSwitch' & $sNL

    ClipPut($sDump)
;~  ConsoleWrite($sDump & $sNL)
EndFunc

Would be nice if this could be done with a little bit less code. :)

Not sure for what ... but thats normal. ;)

[fix: Create_SwitchCase(1)]

[add: _Redim(), data checkup]

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

  • 10 months later...

(damn, lost my initial reply message.)

Is just a test case for 'what if you like/need to be able to create array's in any of the possible valid AutoIt dimension counts'.

As arrays can't be soft-code created, you end up with a 1 to 64 condition set. (ignoring possible external code use.)

Only field that I know that tends to uses more than 3 dimensions is in Math. But In those cases AutoIt would probably not be the right choice.

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

(damn, lost my initial reply message.)

Is just a test case for 'what if you like/need to be able to create array's in any of the possible valid AutoIt dimension counts'.

As arrays can't be soft-code created, you end up with a 1 to 64 condition set. (ignoring possible external code use.)

Only field that I know that tends to uses more than 3 dimensions is in Math. But In those cases AutoIt would probably not be the right choice.

Okay, that's a little clearer. I can think of loads of uses for multidimensional arrays outside of maths applications, although I have personally only ever had to use four dimensions. And that was for associating sets of coordinates between several grids, or something along those lines. Can't rememeber the exact details. Edited by czardas
Link to comment
Share on other sites

For Au3Int, I just did the same thing but left it at a maximum of 8 I think. I was struggling to justify adding more.

The same applies to redimming. I wish there were easier ways to modify arrays, but then there is not usually any reason to.

Erm. Yea, The lack of 'practical' reasons for it is somewhat of a hurtle. (I generally tend to ignore that part.)

I can think of loads of uses for multidimensional arrays outside of maths applications.

Feel free to add some info, or relative links, about those uses (if or when you have/find them). Alway good to have some possible/additional purpose idea's to go with this topic. 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

Okay here's a link. FEN SAN and PGN I have an array called $xpath with 4 dimensions. I didn't know the correct syntax for array declaration at the time, but it doesn't really effect the script. There's plenty of comments in there anyhow. Whilst looking at this script again, I notice that it would be perfectly justified to add a fifth dimension, as each element holds two coordinates.

It would be possible to write this script without storing coordinates in such an array, however it saves a lot of time when parsing multiple games. I hope to do something with this script eventually. I'm kind of stuck with dragging and dropping a chess piece from one grid label to another. I decided to come back to it in time. However I may just forego the luxury afforded by dragging and force the user to enter a valid FEN string instead. We'll see!

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