czardas

_ArrayForceDim

11 posts in this topic

#1 ·  Posted (edited)

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
1 person likes this

Share this post


Link to post
Share on other sites

#2 ·  Posted (edited)

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

#3 ·  Posted (edited)

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

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

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

#7 ·  Posted (edited)

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

#9 ·  Posted (edited)

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

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

#11 ·  Posted (edited)

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

    • ronmage
      By ronmage
      So I have a loop that keeps reading data from an array and searching it for the same value. If the value is no there it does work then adds the value to the array to prevent it from doing the same work.
      If _ArraySearch($ID,$filearray[$i]) = -1 Then Work.... _ArrayAdd($ID,$filearray[$i]) EndIf This is in a for loop hence $i
      So what is happening is the code works great for several hours. After a period of time _ArraySearch($ID,$filearray[$i]) will result in -1 even if $ID = $filearray. So it ready as if there is no data in the array. Anyone have this problem? 
       
      Also I am just running in using F5 not compiling it and running it if that makes a difference.
       
    • Ascer
      By Ascer
      1. Description.
      Udf working with MSDN System.Collections.ArrayList. Allow you to make fast operations on huge arrays, speed is even x10 better than basic _ArrayAdd.  Not prefered for small arrays < 600 items. 2. Requirements
      .NET Framework 1.1+ System Windows 3. Possibilities.
      ;=============================================================================================================== ; UDF Name: List.au3 ; ; Date: 2018-02-17, 10:52 ; Description: Simple udf to create System Collections as ArrayList and make multiple actions on them. ; ; Function(s): _ListCreate -> Creates a new list ; _ListCapacity -> Gets a list size in bytes ; _ListCount -> Gets items count in list ; _ListIsFixedSize -> Get bool if list if fixed size ; _ListIsReadOnly -> Get bool if list is read only ; _ListIsSynchronized -> Get bool if list is synchronized ; _ListGetItem -> Get item on index ; _ListSetItem -> Set item on index ; ; _ListAdd -> Add item at end of list ; _ListClear -> Remove all list items ; _ListClone -> Duplicate list in new var ; _ListContains -> Get bool if item is in list ; _ListGetHashCode -> Get hash code for list ; _ListGetRange -> Get list with items between indexs ; _ListIndexOf -> Get index of item ; _ListInsert -> Insert a new item on index ; _ListInsertRange -> Insert list into list on index ; _ListLastIndexOf -> Get index last of item ; _ListRemove -> Remove first found item ; _ListRemoveAt -> Remove item in index ; _ListRemoveRange -> Remove items between indexs ; _ListReverse -> Reverse all items in list ; _ListSetRange -> Set new value for items in range ; _ListSort -> Sort items in list (speed of reading) ; _ListToString -> Get list object name ; _ListTrimToSize -> Remove unused space in list ; ; Author(s): Ascer ;=============================================================================================================== 4. Downloads
      List.au3 5. Examples
      SpeedTest _ArrayAdd vs ListAdd SpeedTest ArraySearch vs ListIndexOf Basic usage - crating guild with members  
    • FMS
      By FMS
      Hello,
      I'm trying to read a binary file to an array but couln't get it to work.
      Also I coul not find any help in the forum around this subject whish was helpfull.
      Is there any way it could be done?
      I tried a lot of ways but maybe somebody know's the right way?
      #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <File.au3> #include <Array.au3> #include <AutoItConstants.au3> Local $in=FileOpen("TEST_labels.idx1-ubyte",16) ; 16+0=Read binary Local $data = FileRead($in) Local $FileArray = BinaryToString($data,4) ;~ $FileArray = StringSplit($BinarydData, @CRLF, 1+2) ;~ Local $FileArray = StringRegExp($BinarydData, "[^\r\n]+", 3) FileClose($in) _ArrayDisplay($FileArray,"$FileArray","",32) MsgBox($MB_SYSTEMMODAL, "", "$FileArray = " & $FileArray )  
      TEST_labels.idx1-ubyte
    • ur
      By ur
      I am reading a CSV file which is tab seperated as below.
      Local $aArray = FileReadToArray($file) And now, I am splitting this main array record wise so that Array contains internally another arrow to represent each row.
      For $i = 0 to (UBound($aArray) - 1) ;MsgBox(0,"",$aArray[$i]) $aArray[$i] = StringSplit(StringStripCR($aArray[$i]), Chr(9),2);Chr(9) for tab ;_ArrayDisplay($aArray[$i]) Next Afther that, _ArrayDIsplay is able to see the individual internal arrays.
      _ArrayDisplay($aArray[1]) But If I try to access the individual element of it as below.It is not showing any result.
      MsgBox(0,"",$aArray[1][1]) Any suggestion, below is the sample csv file.
      New Text Document.csv
    • Jibsbrown
      By Jibsbrown
      Need some help understanding why the ConsoleWrite works inside 2nd For loop but not out side. Between Audit Wiki, Help file , Forum searching (lots of code reading), and YouTube ( shout out to TutsTeach), I have not been able to find the reason why. 
      $sIniPath = "installLog.ini" ; - Get section name $iniSctionNames = IniReadSectionNames($sIniPath) ; - Get Keys and Vaules For $a = 1 to UBound($iniSctionNames) - 1 $keys = IniReadSection($sIniPath , $iniSctionNames[$a]) For $b = 1 to UBound($keys) - 1 $oldSysInfo = IniRead($sIniPath , $iniSctionNames[1], $keys[$b][0], "") $PntIPInfo = IniRead($sIniPath , $iniSctionNames[2], $keys[$b][0], "") $NewPCInfor = IniRead($sIniPath , $iniSctionNames[3], $keys[$b][0], "") ;ConsoleWrite($oldSysInfo & @LF) Next ;ConsoleWrite($oldSysInfo & @LF) Next ConsoleWrite($oldSysInfo) My intention is to use the variables later for Listboxes. Any explanation, forum post links or whatever would help. Sorry also very very new to Autoit.
      Also here's the ini file.
      [OldSysInfo] 4=192.168.0.4|DESKTOP-RDIU2SN|R90M05Q8 5=192.168.0.5|SD0123456789101|R9WGP9P 6=192.168.0.6|SD0123456789102|R9WGP9PT 3=192.168.0.3|DESKTOP-3RS4LKL|R9WGP9P 23=192.168.0.23|SD0123456789102|MXL1234P5I [PrinterIp] 50=192.168.0.50 48=192.168.0.48 47=192.168.0.47 [NewSysInfo] newPC = SD0123456789adfs|192.168.0.185|2UA1234FTR Thank you for your time.