Jump to content
czardas

_ArrayForceDim

Recommended Posts

czardas

A question came up regarding _ArrayTranspose(). Strictly speaking this function is only suitable for 2D arrays:  transposition of a 1D array should return the same array, or an error. Why? - because there is no second dimension available to make the transposition. The workaround for this is to force the return of a 2D array containing a single row, which is all well and good until you want to reverse the process.  Should the original 1D array be returned, or a 2D array containing a single column?

A Possible Solution

After some discussion in the MVP forum, I have concluded that this type of problem is more common than people are led to believe. The help file clearly states that ReDim does not allow you to modify the number of dimensions in an array, and nobody questions this. Here is a function (similar to ReDim) which allows you to return the first column of a 2D array as a 1D array (and similar changes). You can add, or remove, up to 5 dimensions. The maximum supported number of dimensions is 6. Arrays with more than 4 dimensions are very rare.

#include <Array.au3> ; For _ArrayDisplay

Global $aArray1D[5] = [10, 20, 30, 40, 50]
Global $aArray2D[3][5] = [[10, 20, 30, 40, 50], [11, 21, 31, 41, 51], [12, 22, 32, 42, 52]]
Global $aArray3D[3][4][2] = [[["000", "001"], ["010", "011"], ["020", "021"], ["030", "031"]], _
                           [["100", "101"], ["110", "111"], ["120", "121"], ["130", "131"]], _
                           [["200", "201"], ["210", "211"], ["220", "221"], ["230", "231"]]]

_ArrayForceDim($aArray3D, 2) ; Convert to 2D
_ArrayDisplay($aArray3D, '3D coverted to 2D')

_ArrayDisplay($aArray2D, "2D (before)")
_ArrayForceDim($aArray2D, 1)
_ArrayDisplay($aArray2D, "2D converted to 1D (after)")

_ArrayForceDim($aArray1D, 6) ; 1D converted to 6D
For $i = 0 To UBound($aArray1D, 1) -1
    ConsoleWrite($aArray1D[$i][0][0][0][0][0] & @LF)
Next

; #FUNCTION# ===================================================================================================================
; Name...........: _ArrayForceDim
; Description ...: Changes the size of an array by adding, or removing, dimensions.
; Syntax.........: _ArrayForceDim($aArray, $iDimensions)
; Parameters.....; $aArray - The original array.
;                  $iDimensions - The number of dimensions in the returned array.
; Return values .: Returns the modified array ByRef.
;                  Failure sets @error as follows:
;                  |@error = 1 The first parameter is not an array.
;                  |@error = 2 The requested array has more than 6 dimensions.
;                  |@error = 3 The original array has more than 6 dimensions.
; Author.........: czardas
; Comments ......; This function works for up to 6 dimensions.
;                  New dimensions are added at the end in a standard sequence: $aArray[7][6] may become $aArray[7][6][1]
;                  Dimensions are removed in reverse sequence: $aArray[7][6] will become $aArray[7]
; ==============================================================================================================================

Func _ArrayForceDim(ByRef $aArray, $iDimensions)
    If Not IsArray($aArray) Then Return SetError(1)

    $iDimensions = Int($iDimensions)
    If $iDimensions < 1 Or $iDimensions > 6 Then Return SetError(2)

    Local $iPreDims = UBound($aArray, 0) ; current number of dimensions
    If $iPreDims = $iDimensions Then Return ; no change
    If $iPreDims > 6 Then Return SetError(3) ; too many dimensions

    ; get the size of each original dimension
    Local $a[$iPreDims +1]
    For $i = 1 To $iPreDims
        $a[$i] = UBound($aArray, $i)
    Next

    ReDim $a [$iDimensions +1] ; modify the number of dimensions

    ; assign the size of each new dimension ...[1][1][1] etc...
    For $i = $iPredims + 1 To $iDimensions
        $a[$i] = 1
    Next

    Local $2 = 0, $3 = 0, $4 = 0, $5 = 0, $6 = 0 ; varibale names are associated with dimensions
    Local $sElement = '$aArray' & StringLeft('[$1][$2][$3][$4][$5][$6]', $iPredims * 4) ; to access the original array elements

    ; add or remove dimensions
    Switch $iDimensions
        Case 1
            Local $aNewArray[$a[1]]
            For $1 = 0 To $a[1] -1
                $aNewArray[$1] = Execute($sElement)
            Next

        Case 2
            Local $aNewArray[$a[1]][$a[2]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    $aNewArray[$1][$2] = Execute($sElement)
                Next
            Next

        Case 3
            Local $aNewArray[$a[1]][$a[2]][$a[3]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        $aNewArray[$1][$2][$3] = Execute($sElement)
                    Next
                Next
            Next

        Case 4
            Local $aNewArray[$a[1]][$a[2]][$a[3]][$a[4]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        For $4 = 0 To $a[4] -1
                            $aNewArray[$1][$2][$3][$4] = Execute($sElement)
                        Next
                    Next
                Next
            Next

        Case 5
            Local $aNewArray[$a[1]][$a[2]][$a[3]][$a[4]][$a[5]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        For $4 = 0 To $a[4] -1
                            For $5 = 0 To $a[5] -1
                                $aNewArray[$1][$2][$3][$4][$5] = Execute($sElement)
                            Next
                        Next
                    Next
                Next
            Next

        Case 6
            Local $aNewArray[$a[1]][$a[2]][$a[3]][$a[4]][$a[5]][$a[6]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        For $4 = 0 To $a[4] -1
                            For $5 = 0 To $a[5] -1
                                For $6 = 0 To $a[6] -1
                                    $aNewArray[$1][$2][$3][$4][$5][$6] = Execute($sElement)
                                Next
                            Next
                        Next
                    Next
                Next
            Next
    EndSwitch

    $aArray = $aNewArray
EndFunc ;==> _ArrayForceDim

In terms of expanding an array, this function is useful when you know how many dimensions are needed, but you don't know how large the array will eventually become. Dimensions are added with the minimum size.

Edited by czardas
  • Like 1

Share this post


Link to post
Share on other sites
czardas

Now for a more realistic example.

#include <Array.au3> ; For _ArrayDisplay

; create a list of subjects
Local $aSubjects = [['Art',     'Painting', 'Music',   'Drama',     'Dance'], _
                    ['Science', 'Maths',    'Physics', 'Chemistry', 'Biology' ], _
                    ['Sport',   'Football', 'Golf',    'Snooker',   'Chess']]

_ArrayDisplay($aSubjects)

; create an (out of sequence) table of data pertaining to gender
Local $aGender = [['Gender', 'Painting', 'Physics', 'Music', 'Chess', 'Drama', 'Dance', 'Biology', 'Football', 'Golf', 'Maths', 'Chemistry', 'Snooker'], _
                  ['Male',    2,          5,         2,       10,      3,       1,       5,         12,         4,      6,       5,           1], _
                  ['Female',  7,          1,         3,       0,       4,       10,      6,         8,          0,      6,       6,           1]]

_ArrayDisplay($aGender)

; let's add another dimension
_ArrayForceDim($aSubjects, 3)

; we want three tables stacked so...
ReDim $aSubjects [UBound($aSubjects, 1)] [UBound($aSubjects, 2)] [3]

; add data according to the given information
For $k = 1 To UBound($aGender, 2) -1
    For $i = 0 To UBound($aSubjects) -1
        For $j = 1 To UBound($aSubjects, 2) -1
            If $aSubjects[$i][$j][0] = $aGender[0][$k] Then
                $aSubjects[$i][$j][1] = $aGender[1][$k]
                $aSubjects[$i][$j][2] = $aGender[2][$k]
                ContinueLoop 2
            EndIf
        Next
    Next
Next

; let's check the better reorganized data
For $i = 0 To UBound($aSubjects) -1
    ConsoleWrite('Category = ' & $aSubjects[$i][0][0] & @LF)
    For $j = 1 To UBound($aSubjects, 2) -1
        ConsoleWrite('Boys attending '  & $aSubjects[$i][$j][0] & ' = ' & $aSubjects[$i][$j][1] & @LF & _
                     'Girls attending ' & $aSubjects[$i][$j][0] & ' = ' & $aSubjects[$i][$j][2] & @LF & @LF)
    Next
Next

; #FUNCTION# ===================================================================================================================
; Name...........: _ArrayForceDim
; Description ...: Changes the size of an array by adding, or removing, dimensions.
; Syntax.........: _ArrayForceDim($aArray, $iDimensions)
; Parameters.....; $aArray - The original array.
;                  $iDimensions - The number of dimensions in the returned array.
; Return values .: Returns the modified array ByRef.
;                  Failure sets @error as follows:
;                  |@error = 1 The first parameter is not an array.
;                  |@error = 2 The requested array has more than 6 dimensions.
;                  |@error = 3 The original array has more than 6 dimensions.
; Author.........: czardas
; Comments ......; This function works for up to 6 dimensions.
;                  New dimensions are added at the end in a standard sequence: $aArray[7][6] may become $aArray[7][6][1]
;                  Dimensions are removed in reverse sequence: $aArray[7][6] will become $aArray[7]
; ==============================================================================================================================

Func _ArrayForceDim(ByRef $aArray, $iDimensions)
    If Not IsArray($aArray) Then Return SetError(1)

    $iDimensions = Int($iDimensions)
    If $iDimensions < 1 Or $iDimensions > 6 Then Return SetError(2)

    Local $iPreDims = UBound($aArray, 0) ; current number of dimensions
    If $iPreDims = $iDimensions Then Return ; no change
    If $iPreDims > 6 Then Return SetError(3) ; too many dimensions

    ; get the size of each original dimension
    Local $a[$iPreDims +1]
    For $i = 1 To $iPreDims
        $a[$i] = UBound($aArray, $i)
    Next

    ReDim $a [$iDimensions +1] ; modify the number of dimensions

    ; assign the size of each new dimension ...[1][1][1] etc...
    For $i = $iPredims + 1 To $iDimensions
        $a[$i] = 1
    Next

    Local $2 = 0, $3 = 0, $4 = 0, $5 = 0, $6 = 0 ; varibale names are associated with dimensions
    Local $sElement = '$aArray' & StringLeft('[$1][$2][$3][$4][$5][$6]', $iPredims * 4) ; to access the original array elements

    ; add or remove dimensions
    Switch $iDimensions
        Case 1
            Local $aNewArray[$a[1]]
            For $1 = 0 To $a[1] -1
                $aNewArray[$1] = Execute($sElement)
            Next

        Case 2
            Local $aNewArray[$a[1]][$a[2]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    $aNewArray[$1][$2] = Execute($sElement)
                Next
            Next

        Case 3
            Local $aNewArray[$a[1]][$a[2]][$a[3]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        $aNewArray[$1][$2][$3] = Execute($sElement)
                    Next
                Next
            Next

        Case 4
            Local $aNewArray[$a[1]][$a[2]][$a[3]][$a[4]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        For $4 = 0 To $a[4] -1
                            $aNewArray[$1][$2][$3][$4] = Execute($sElement)
                        Next
                    Next
                Next
            Next

        Case 5
            Local $aNewArray[$a[1]][$a[2]][$a[3]][$a[4]][$a[5]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        For $4 = 0 To $a[4] -1
                            For $5 = 0 To $a[5] -1
                                $aNewArray[$1][$2][$3][$4][$5] = Execute($sElement)
                            Next
                        Next
                    Next
                Next
            Next

        Case 6
            Local $aNewArray[$a[1]][$a[2]][$a[3]][$a[4]][$a[5]][$a[6]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        For $4 = 0 To $a[4] -1
                            For $5 = 0 To $a[5] -1
                                For $6 = 0 To $a[6] -1
                                    $aNewArray[$1][$2][$3][$4][$5][$6] = Execute($sElement)
                                Next
                            Next
                        Next
                    Next
                Next
            Next
    EndSwitch

    $aArray = $aNewArray
EndFunc ;==> _ArrayForceDim

After organizing the data in a sensible way, you realize that you also need to store data from several different institutions. The 3rd dimension containing students needs to contain the names of institutions, and the number of students of each gender need to be placed in the fourth dimension. Since the data is quite well organized, you can simply add another dimension and overwrite the third dimension using the same method.

This is just an example of a way of coding using multidimensional arrays. More complex examples may involve modification to arrays in real time, according to new information; or greater (or less) detail may suddenly be needed. You may wish to work with a multidimensional array of main headings and only expand the number of dimensions occasionally: so as to preserve memory. You can delete the extra dimensions when you have finished with them - returning to the original array.

This is not the only way to organize data, but sometimes it might be a most suitable approach. The extra dimensions are available, so let's use them!

Edited by czardas

Share this post


Link to post
Share on other sites
czardas

I'm not quite ready to update the first post yet, but I have modified this by adding an optional parameter. When $bPush is set to true, and you increase the number of dimensions; instead of adding higher dimensions with minimum bounds, the original array structure is pushed upwards towards the higher dimensions and new lower dimensions are inserted in the vacated positions (left of the array declaration syntax). When reversing this process, the lower dimensions are deleted from the left of the array declaration syntax; and the array structure returns back to, or goes beyond, the original dimensions. Meh, just run the examples and you'll see what I mean!

#include <Array.au3>

; Example
Local $aData = ['Header 1','Header 2','Header 3','Header 4','Header 5','Header 6']
_ArrayDisplay($aData, "Original 1D")
_ArrayForceDim($aData, 2, True) ; Push the 1st dimension to the 2nd dimension
_ArrayDisplay($aData, "1D to 2D push")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; Quick Test
Local $a2D = [[1,2,3],['a','b','c']]
_ArrayDisplay($a2D, "Quick Test")

_ArrayForceDim($a2D, 4, True) ; push the 1st and 2nd dimensions to the 3rd and 4th dimensions
MsgBox(0, "No of Dimensions", UBound($a2D, 0))

For $i = 0 To UBound($a2D, 1) - 1
    For $j = 0 To UBound($a2D, 2) - 1
        For $k = 0 To UBound($a2D, 3) - 1
            For $l = 0 To UBound($a2D, 4) - 1
                ConsoleWrite($a2D[$i][$j][$k][$l] & @LF)
            Next
        Next
    Next
Next

_ArrayForceDim($a2D, 2, True)
_ArrayDisplay($a2D, "4D to 2D reverse push")

_ArrayForceDim($a2D, 1, True)
_ArrayDisplay($a2D, "2D to 1D reverse push")

_ArrayForceDim($a2D, 2, True)
_ArrayDisplay($a2D, "1D to 2D push")

; the very first element always remains intact
_ArrayForceDim($a2D, 1)
_ArrayDisplay($a2D, "2D to 1D default")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


; #FUNCTION# ===================================================================================================================
; Name...........: _ArrayForceDim
; Description ...: Changes the size of an array by adding, or removing, dimensions.
; Syntax.........: _ArrayForceDim($aArray, $iDimensions [, $iPush = False])
; Parameters.....; $aArray - The original array.
;                  $iDimensions - The number of dimensions in the returned array.
;                  $bPush - [Optional] If set to True, new dimensions are inserted, or removed from the left [see comments].
; Return values .: Returns the modified array ByRef.
;                  Failure sets @error as follows:
;                  |@error = 1 The first parameter is not an array.
;                  |@error = 2 The requested array has more than 7 dimensions.
;                  |@error = 3 The original array has more than 7 dimensions.
; Author.........: czardas
; Comments ......; This function works for up to 7 dimensions.
;                  By default, new dimensions are added to the right in a standard sequence: $aArray[7][6] ==> $aArray[7][6][1]
;                  Dimensions are removed in reverse sequence: $aArray[7][6] ==> $aArray[7]
;                  When the $bPush parameter is set to True, the original array will be pushed to higher dimensions:
;                  $aArray[7][6] ==> $aArray[1][7][6], or the process reversed: $aArray[7][6] ==> $aArray[6]
; ==============================================================================================================================

Func _ArrayForceDim(ByRef $aArray, $iDimensions, $bPush = False)
    If Not IsArray($aArray) Then Return SetError(1)

    $iDimensions = Int($iDimensions)
    If $iDimensions < 1 Or $iDimensions > 7 Then Return SetError(2)

    Local $iPreDims = UBound($aArray, 0) ; current number of dimensions
    If $iPreDims = $iDimensions Then Return ; no change
    If $iPreDims > 7 Then Return SetError(3) ; too many dimensions

    ; get the size of each original dimension
    Local $a[$iPreDims +1]
    For $i = 1 To $iPreDims
        $a[$i] = UBound($aArray, $i)
    Next

    Local $sElement = '[$1][$2][$3][$4][$5][$6][$7]' ; array syntax used to access the original array elements

    If $bPush Then ; prefix dimensions, or delete from the left
        Local $iOffset = Abs($iDimensions - $iPreDims)
        If $iPreDims > $iDimensions Then
            For $i = 1 To $iDimensions ; shift elements to lower indices
                $a[$i] = $a[$i + $iOffset]
            Next
            $sElement = '$aArray' & StringLeft('[0][0][0][0][0][0]', $iOffset * 3) & StringLeft($sElement, $iDimensions * 4)

        Else
            ReDim $a [$iDimensions +1] ; make space for more dimensions
            For $i = $iDimensions To $iOffset +1 Step -1 ; shift elements to higher indices
                $a[$i] = $a[$i - $iOffset]
            Next
            For $i = 1 To $iOffset ; assign the size of each additional dimension [1][1][1]... etc...
                $a[$i] = 1
            Next

            $sElement = '$aArray' & StringMid($sElement, 1 + $iOffset * 4, $iPredims * 4)
        EndIf

    Else ; Default behaviour = append dimensions, or delete from the right
        ReDim $a [$iDimensions +1] ; modify the number of dimensions

        For $i = $iPredims + 1 To $iDimensions ; assign the size of each new dimension ...[1][1][1] etc...
            $a[$i] = 1
        Next
        $sElement = '$aArray' & StringLeft($sElement, $iPredims * 4)

        Local $2 = 0, $3 = 0, $4 = 0, $5 = 0, $6 = 0, $7 = 0 ; required to reference new higher dimensions
    EndIf

    ; add or remove dimensions
    Switch $iDimensions
        Case 1
            Local $aNewArray[$a[1]]
            For $1 = 0 To $a[1] -1
                $aNewArray[$1] = Execute($sElement)
            Next

        Case 2
            Local $aNewArray[$a[1]][$a[2]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    $aNewArray[$1][$2] = Execute($sElement)
                Next
            Next

        Case 3
            Local $aNewArray[$a[1]][$a[2]][$a[3]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        $aNewArray[$1][$2][$3] = Execute($sElement)
                    Next
                Next
            Next

        Case 4
            Local $aNewArray[$a[1]][$a[2]][$a[3]][$a[4]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        For $4 = 0 To $a[4] -1
                            $aNewArray[$1][$2][$3][$4] = Execute($sElement)
                        Next
                    Next
                Next
            Next

        Case 5
            Local $aNewArray[$a[1]][$a[2]][$a[3]][$a[4]][$a[5]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        For $4 = 0 To $a[4] -1
                            For $5 = 0 To $a[5] -1
                                $aNewArray[$1][$2][$3][$4][$5] = Execute($sElement)
                            Next
                        Next
                    Next
                Next
            Next

        Case 6
            Local $aNewArray[$a[1]][$a[2]][$a[3]][$a[4]][$a[5]][$a[6]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        For $4 = 0 To $a[4] -1
                            For $5 = 0 To $a[5] -1
                                For $6 = 0 To $a[6] -1
                                    $aNewArray[$1][$2][$3][$4][$5][$6] = Execute($sElement)
                                Next
                            Next
                        Next
                    Next
                Next
            Next

        Case 7
            Local $aNewArray[$a[1]][$a[2]][$a[3]][$a[4]][$a[5]][$a[6]][$a[7]]
            For $1 = 0 To $a[1] -1
                For $2 = 0 To $a[2] -1
                    For $3 = 0 To $a[3] -1
                        For $4 = 0 To $a[4] -1
                            For $5 = 0 To $a[5] -1
                                For $6 = 0 To $a[6] -1
                                    For $7 = 0 To $a[7] -1
                                        $aNewArray[$1][$2][$3][$4][$5][$6][$7] = Execute($sElement)
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
            Next
    EndSwitch

    $aArray = $aNewArray
EndFunc ;==> _ArrayForceDim

The first element ([0], [0][0], [0][0][0], [0][0][0][0] etc...) will never be overwritten regardless of how this function is used. Also note that the conversion in the first example (of a 1D array to a 2D array with the 3rd parameter set to True) returns exactly the same result as when a 1D array is passed to _ArrayTranspose().

Inserting new dimensions mid-between existing dimensions is not on the radar; but prefixing (or appending) new dimensions to existing dimensions introduces another approach to working with arrays, currently unexplored by me.

Edited by czardas

Share this post


Link to post
Share on other sites
jchd
Quote

The help file clearly states that ReDim does not allow you to modify the number of dimensions in an array, and nobody questions this.

I did and still do!

I've been long advocating that an array containing, say, 24 elements should be REDIM-able at will and at no significant penalty to any array whose product of dimensions is 24. E.g. $a[24] <--> $a[12][2] <--> $a[1][24] <--> $a[3][8] <--> $a[4][2][3] <--> $a[2][1][3][1][2][1][2] and so on.

Transposition should also be an internal operation as well, essentially free on performance penalty either.  Of course transposition should be allowed for any combination of dimensions, the only burden being for users to get the transposition signature right and not mix up their own representation.

Finally, I find the limit of 16*1024*1024 elements well too low for many practical use cases.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
czardas

That's an interesting proposal, although quite a different approach. I suppose you would loop from low to high until you ran out of elements from either the source or the target array (Flood Fill). Transposition in higher dimensions seems tricky. It's that signature you mentioned that sounds rather scary. How many orientations to choose from? :frantics:

Share this post


Link to post
Share on other sites
jchd

Doing free array redimensionning is just dead easy and requires zero loop. It's just indices' game which can be carried out with a few lines of fast C[++] internal code. Look, say you declare an array like this: $a[24]

Then $a[9] is the 9 + 1 = 10th element in the array (see it as 1-based internally).
ReDim $a[3][8]
Then $a[1][5] is the ((1 * 3) + 5 + 1) = 9th element in the original array (untouched).
ReDim $a[4][2][3]
Then $a[2][0][1] is the ((2 * 2 * 3) + (0 * 2) + 1 + 1) = 14th element in the original array (still untouched).

This works even with dummy dimensions if needed, e.g. ReDim $a[3][1][2][1][4] and don't tell me that it would slow down AutoIt more than having to duplicate arrays element by element in nested loops with pedestrian AutoIt code, like we're curently forced to do.

Dummy dimensions ARE useful. Note that the conversion from a column-vector to a row-vector can be done without question: ReDim from $a[24] to $a[1][24].

For transposition: say that by convention dimensions are numbered from 1 upwards. Transpose($a, 3, 1, 2) would only reorder last redim indices, which can also be done internally very easily in fast code. Transposition and redimensionning are not the same operation.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
czardas

Yep, just an indices game - similar to the one I play above - before assigning values at new locations (using loops).

I'm totally open to the practicalities of other implementations. The implementation I decided on here was influenced by both the question in GH&S and circumstances where I would have used this previously myself (if it had existed in AutoIt). It makes construction of complex multidimensional arrays easier; by allowing you to assign the main categories, or ID information, separate from the array contents. Headings and sub-headings can be added at any stage during construction, by pushing the array upwards towards higher dimensions (making space to accommodate). This is a subset of possible implementations, and your plan is another subset which overlaps mine to a degree.

The fact that _ArrayTranspose() with 1D arrays returns the same result as 1D to 2D (push) is simply down to algorithm collision (for want of a better term). I must say that I find what you have to say quite fascinating. The current limitation of AutoIt ReDim is not something that worries me so much though. I think fixing problems with numbers is a huge priority. Things like arrays are just icing on the cake (well, icing with a cherry :) ).

Edited by czardas

Share this post


Link to post
Share on other sites
jchd
2 hours ago, czardas said:

I think fixing problems with numbers is a huge priority. Things like arrays are just icing on the cake (well, icing with a cherry :) ).

[Indeed the new partial quote is useful]

I don't agree so much: issues with large integers is a marginal issue, idiosyncrasies between Ints and FPs we can relatively easily live with, but arrays are central to almost any serious script. Look at how many times you'd like to get a 2D array directly from a regexp, parsing a CSV or such. Regularly, the point is "how do I sort a 2D array by rows (not by column)", reformatting/rearranging data fetched from Excel or a database, etc.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
czardas

Hmm, I guess you're right. It's not just big Integers though. the operators are doing some strange things. As you well know; multiplication and division are the same thing, or opposites, if you prefer. Okay, I know we can convert floats to integers and do lots of clever things to patch the maths. I would prefer if division was the true reverse of multiplication. It seems we are missing some basic axioms. Although I am not really so experienced in programming and perhaps what I ask is more complicated than I imagine.

We'll stick to discussing arrays then! :P

Edited by czardas

Share this post


Link to post
Share on other sites
jchd

Did you mean disgusting or dissecting?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
czardas

None of my arrays are like that, at least I don't think they are. :tv_horror:

Edited by czardas

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

  • Similar Content

    • TrashBoat
      By TrashBoat
      So Im trying to make a simple 2d game and make some sort of collision detection so why not to make a 2 dimensional array but i have no clue how  to write it in multiple lines
      Global $map[5,5] = [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0 _ [0,0,0,0,0] something like this but it doesn't work
    • Zein
      By Zein
      #include "..\Include\Array.au3" #include "..\Include\File.au3" #include "..\Include\AutoItConstants.au3" Local $aRetArray Local $sFilePath = "n.csv" _FileReadToArray($sFilePath, $aRetArray, ",") ; _FileReadToArray($sFilePath, $aRetArray, $FRTA_COUNT, ",") _ArrayDisplay($aRetArray, "Original", Default, 8) The above code shows two versions of _FileReadToArray and both don't work as expected.
      The first one doesn't use the comma as a delimiter. (so I get a single column array)  I tried adding "Default" between $aRetArray and "," then it told me it had an incorrect number of parameters. 

      I looked again at the documentation:
       
      #include <File.au3> _FileReadToArray ( $sFilePath, ByRef $vReturn [, $iFlags = $FRTA_COUNT [, $sDelimiter = ""]] )
      And I with or without the flags params I should be getting a 2D array due to my file being a csv. 
      I then tried a regular flag, $FRTA_COUNT, and it tells me that I'm using a variable $FRTA_COUNT while it's not declared. Tried putting in 1 instead and it told me again, incorrect number of params. 

       
    • ternal
      By ternal
      Hi,
      Recently I have had the need to do a sort and then do a second sort while the item of the first sort stays the same ( double sorting , first on column x then while column x is the same sort column y).
      I did not put much efffort into error checking but so far I did not need it.
      For my applications so far it works perfectly however if someone is willing I want to test this extensivly.
      If anyone has big lists of random stuff to sort could you try this out please?
      #include <Array.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _ArraySort_Double ; Description ...: ; Syntax ........: _ArraySort_Double (Byref $array[, $first_index = Default[, $second_index = Default[, $ascending = Default]]]) ; Parameters ....: $array - 2d array to sort. ; $first_index - [optional] first column to sort. Default is 0. ; $second_index - [optional] second column to sort. Default is 1. ; $ascending - [optional] ascending/descending. Default is 1. ; Return values .: 1 if no errors occured , -1 if errors occured ; Author ........: Ternal ; Remarks .......: Needs excessive testing. ; Related .......: _arraysort() ; =============================================================================================================================== Func _ArraySort_Double (byref $array, $first_index = Default, $second_index = Default, $ascending = Default) Local $temp_value Local $counter = 1 If UBound($array, $UBOUND_DIMENSIONS) <> 2 Then MsgBox(0, "error", "error") return -1 EndIf If $first_index = Default Then $first_index = 0 If $second_index = Default Then $second_index = 1 If $ascending = Default Then $ascending = 1 _ArraySort($array, $ascending, 0, 0, $first_index); you can alter settings of primary sort here If @error Then MsgBox(0, "error", @error) return -1 EndIf $temp_value = $array[0][$first_index] For $x = 1 to UBound($array, 1) - 1 If Mod( $x, 10000) = 0 Then ConsoleWrite("at " & $x & " of a total : " & UBound($array, 1) & @CRLF) If $array[$x][$first_index] = $temp_value Then $counter+= 1 If $x = UBound($array, 1) - 1 Then; do last line here(if last line is not a new item) _ArraySort($array, $ascending, $x - $counter, $x, $second_index);you can alter settings of secondary sort here(don't forget to place line 34 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf EndIf Else If $counter > 0 Then ;at least 2 of the same _ArraySort($array, $ascending, $x - $counter, $x - 1, $second_index);you can alter settings of secondary sort here(don't forget to place line 29 the exact same) If @error Then MsgBox(0, "error", @error) return -1 EndIf $counter = 1 EndIf EndIf $temp_value = $array[$x][$first_index] Next Return 1 EndFunc Kind regards, Ternal
    • TrashBoat
      By TrashBoat
      So I've made this script that detects how long i have held down my left mouse button for and stores the information in an array and then sorts its using _ArraySort but the output is half sorted half broken.
      Here's my script:
      HotKeySet("{F1}","_exit") #include <Misc.au3> #include <Timers.au3> #include <Array.au3> Local $dll = DllOpen("user32.dll") $on = False Global $array[0] While(1) If _IsPressed(01,$dll) Then $timer = _Timer_Init() While _IsPressed(01,$dll) Sleep(1) WEnd $time = _Timer_Diff($timer) _ArrayAdd($array,"Time: " & Floor($time) & " ms") ;~ ConsoleWrite("Time: " & Floor($time) & " ms" & @CRLF) EndIf Sleep(50) WEnd Func _exit() _ArraySort($array) _ArrayDisplay($array) Exit EndFunc And the output:

      See how its not sorted?  What is the problem here?
    • MrCheese
      By MrCheese
      Hi guys,
      See attached for an array example.
      to simplify what i want to achieve,  I want to split this array into 9 different csv files.
      the first file would contain the list of "key" and the corresponding "ID1", the second would have "key" and the "ID2", the third would have "key" and "ID3"
      However, I want to remove all the rows that don't have an ID recorded in the respective ID2, ID3 4...5...6 etc, so the file only contains row items with a key and the ID.
      Would be the best way to loop through the rows and delete the row if the array field is blank - would I then need to repeat that row ID to check that the row that its replaced is also empty (ie the one after the one I just deleted)? I see this getting messy.
      or _arraySort, and delete everything below the last filled row? <-- this might be best?
      Or should I use the excel UDF, apply a filter (not selecting the blanks), then create/export to the array->csv?
       
      Super keen to hear your thoughts.
      thanks!
       
       
       
       
      IDArray.csv
×