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

    • Skeletor
      By Skeletor
      Hi Virtual People,
      My array works perfectly fine. However, what is the best practice if the line in the array doesn't have the correct amount of columns and if I can add a placeholder?

       
      For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") Next  
    • MrCheese
      By MrCheese
      hi all,
      reviewing the forum, this thread is applicable: 
       
       
      I wanted to know if there is now a better way to do this?
      In essence, I load a tab delimited txt file into an array (works well). I used tab, as some fields in the original csv contains commas.
      However, I needed autoit to manipulate this array, and output it as a csv.
      IF my array contains items with a comma, without double quotes around the field, then how best do I get a csv out of this?
      My current workaround is to filewritefromarray tab delimited, then open it in excel and save as a csv. I will need to check this to see how the address fields behave that contain a comma.
       
      Any thoughts would be appreciated.
       
    • Skeletor
      By Skeletor
      Hi All,

      I would like to know how you would take a FileLineRead and insert it into an array which then inserts it into Excel?
      One thing to know is the files content is broken up, so I only use half of the content within $FileRead1.
      So its imperative that the $value1, $value2, etc variables be used. 
      Code below:
      $FileRead1 = FileReadLine("C:\temp\sample.txt",1) For $count = 1 To _FileCountLines($FileRead1) Step 1 $string = FileReadLine($FileRead1, $count) $input = StringSplit($string, ",", 1) $value1 = $input[1] $value2 = $input[2] $value3 = $input[3] $value4 = $input[4] _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value1, "A1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value2, "B1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value3, "C1") _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $value4, "D1") Next  
    • AnonymousX
      By AnonymousX
      Hello,
      I'm trying to write a script that moves copies excel cells into an array. I'll than manipulate the values and send array into another program. 
      I don't want range to be specific to a workbook, or sheet, or set of cells.
      I want user to be able to highlight desired cells and to copy either normally ("Ctrl+C") or by a hotkey ("Alt+C"). 
      Could someone help me with this?
      Thank you,
      I've tried to write the framework: (edited)
      #include <MsgBoxConstants.au3> #include <Array.au3> #include <Excel.au3> HotKeySet("!v", "Pastedata") While True Sleep(1000) WEnd func Makearray() local $bArray ;User has cells already copied ;Convert clipboard into an array ;I don;t know how excel stores data to clipboard so don;t know how to bring it into array _Arraydisplay($bArray) MsgBox(0,0,$bArray) return $bArray endfunc func Pastedata() Local $aArray MsgBox(0,0,"wait",1) ;make array based on assumption user has already copied a range to clipboard $aArray = Makearray() ;paste code ;don;t worry about this I got the rest endfunc  
    • Dzenan03
      By Dzenan03
      I want to make a while loop, that creates variables based on a array. For thist I created the array $iDsO with the number and the name of folders in an other folder. Every folder has a different name an I want to create variables(arrays) for each folder that show me all the files in that folder. For example: I have the Folder \Folder1. In it there are the Folders \1, \2, \3. In 1, 2 and 3 there are some files(.png). The array for Folder1 is $iDsO and now I want to crate the arrays $iDsO1, $iDsO2 and $iDsO3 with the files in them can I make something like this:
      While $iDs > 0 ;$iDs is the number of files in Folder1>> $iDsO[0] $iDs#here should come the Foldername for example '1'# = _FileListtoArray(@ProgramFilesDir&"\Folder1\"&$iDsO[$iDs]) $iDs = $iDs - 1 Wend So that in the End I have three variabels ($iDs1, $iDs2 and $iDs3)
       
      Is this posible or if not what could I do instead ( I don´t know the number of folders in Folder1 in the begining).
×