Jump to content
Sign in to follow this  
Carlo84

Declaring multi dimentional arrays

Recommended Posts

Hi all, ok i just spend quite sometime reading in the forums about declaring multi dimentional arrays. (funny how over years you can still be clueless about simple things cause you never really needed them)

Anyways i figured it out thanks to posts from 2004 :-p

When declaring arrays this way the Rows are grouped together.

#include <Array.au3>
Dim $MyArray[2][5] = [['Row[0] Col[0]', 'Row[0] Col[1]', 'Row[0] Col[2]', 'Row[0] Col[3]', 'Row[0] Col[4]'], _
        ['Row[1] Col[0]', 'Row[1] Col[1]', 'Row[1] Col[2]', 'Row[1] Col[3]', 'Row[1] Col[4]']]
_ArrayDisplay($MyArray)

Now my question is is there anyway of declaring an multiple dimentional array with the Cols grouped together?

Share this post


Link to post
Share on other sites

Try not to think of them specifically as rows and columns.

You could just as easily swap the initializers to organize your elements differently, It just depends on how You want to organize the data.

#include "array.au3"
Dim $arr1[3][2]=[["First Name","Last Name"],["Spud","W"],["D","jarlo"]]
_ArrayDisplay($arr1)

Dim $arr2[2][3]=[["First Name","Spud","D"],["Last Name","W","jarlo"]]
_ArrayDisplay($arr2)

Maybe I'm not understanding the result you are looking to achieve.

edit:

I once made a function to "transform" a two-dimension array by swapping (rebuilding) the dimensions, effectively converting say $arr[3][2] to $arr[2][3] while retaining it's elements, but it's not on this computer (where I'm posting from). I'll see if I can dig it up later if you think it may be of use to you.

Edited by spudw2k

Share this post


Link to post
Share on other sites

Maybe I'm not understanding the result you are looking to achieve.

Not trying to achieve anything specific but learning a little something about array declaration :-)

Yes i do realize that in fact they are not really cols and rows, and your right, to work with them youll have to let that idea go.

I once made a function to "transform" a two-dimension array by swapping (rebuilding) the dimensions, effectively converting say $arr[3][2] to $arr[2][3] while retaining it's elements, but it's not on this computer (where I'm posting from). I'll see if I can dig it up later if you think it may be of use to you.

No thanks i just needed to know if they could be declared in such a way, and you answered that :-) I might not like it but hey once ya get used to it it wont matter anymore.

Thanks for your help.

Edited by Djarlo

Share this post


Link to post
Share on other sites

in case you change your mind...i found the "transform" (properly dubbed transpose) function.

here it is unsolicited.

edit: It is designed for Two-Dimensional Arrays

edit: Added Function Description/Usage Header and error checking

; #FUNCTION# ====================================================================================================================
; Name...........: _ArrayTranspose
; Description ...: Transpose a two-dimensional array
; Syntax.........: _ArrayTranspose(ByRef $arr)
; Parameters ....: $arr - The array to Transpose
; Return values .: Success - $arr Transposed
;                 Failure - 0, sets @error to:
;                 |1 - $arr is no an array
;                 |2 - $arr is not a two-dimensional array
; Author ........: Spudw2k
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _ArrayTranspose(ByRef $arr)
If Not IsArray($arr) Then Return SetError(1, 0, 0)
If Not UBound($arr,0) = 2 Then Return SetError(2, 0, 0)
Dim $arrTrans[UBound($arr,2)][UBound($arr,1)]
For $x = 0 To UBound($arrTrans,2)-1
    For $y = 0 To UBound($arrTrans)-1
        $arrTrans[$y][$x]=$arr[$x][$y]
    Next
Next
Return $arrTrans
EndFunc

Usage Example:

#include "array.au3"
Dim $array[3][2]=[["First Name","Last Name"],["John","Smith"],["David","Copperfield"]]
_ArrayDisplay($array)
$array = _ArrayTranspose($array)
_ArrayDisplay($array)
Edited by spudw2k

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
Sign in to follow this  

  • Similar Content

    • By Malkey
      _ArrayMultiSort - This function sorts many columns (with optional case sensitivity, ascending or descending, and in any specific order) of an array.

      The following three functions _ArraySort,  __ArrayQuickSort1D,  and __ArrayQuickSort2D from the Array.au3 include file had to be modified to allow case sensitive sorting.
      The modified functions are renamed by appending a "C" for "Case" to each function's name giving _ArraySortC, __ArrayQuickSort1DC, and __ArrayQuickSort2DC. 
      The modifications involve each of these functions having a trailing parameter, $iCase, added, and, within these functions wherever the function "StringCompare" occurs, its third parameter, $iCase, is inserted. So when $iCase = 0, StringCompare is not case sensitive.  When $iCase = 1, StringCompare is case sensitive.
      Here is the UDF containing the functions used to sort arrays with case sensitivity.
      ArrayMultiSortCase.au3
      #include-once ; https://www.autoitscript.com/forum/topic/198148-_arraymultisort-sort-multiple-columns-with-case-sensitivity/ ; File Name: #include <ArrayMultiSortCase.au3> ;#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7 ; #INCLUDES# ========================================================================================================= #include <Array.au3> ; =============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _ArrayMultiSort ; Description ...: Sort a 1D or, sort a 2D array with the ability to specify which columns to sort and which direction to sort for each column. ; Syntax.........: _ArrayMultiSort(ByRef $avArray[, $sSort = '0a'[, $iStartRow = 0[, $iEndRow = 0]]]) ; Parameters ....: $avArray - Array to sort ; $sSort - [optional] The default setting only sorts the first column, column 0, in ascending order. Otherwise, $sSort is ; a string made up of the column sorting order and sort direction and case sensitivity for that column. Each case sensitive ; column and sort direction is separated by a coma. ; A leading "c" with the column# and sort order means that column is to be sorted case sensitive. ; e.g. "1, c2a,0d" means - First sort column 1 in ascending order, then ; sort column 2 in ascending order, with case sensitivity, then ; sort column 0 is sorted in descending order (case insenitive). ; "a" - Ascending order 0-9,A-Z,a-z (Without "a" or "d" default is "a") ; "d" - Descending order z-a,Z-A,9-0 ; "c" - sort that column with case sensitivity. ; When no "c" present, that column is sorted with case insensitivity (default). ; $iStartRow - [optional] Row index of array to start sorting at ; $iEndRow - [optional] Row index of array to stop sorting at ; Return values .: Success - 1 ; Failure - 0, @error set to the same as returned from _ArraySort() function. ; Author ........: Malkey ; Modified.......: ; Remarks .......: Updated version from https://www.autoitscript.com/forum/topic/166426-_filelisttoarray-sorting/?do=findComment&comment=1216234 ; This function requires the __TestExp function, and #Include <Array.au3> and the case sensitive versions of _ArraySort, ; __ArrayQuickSort1D, and __ArrayQuickSort2D. ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func _ArrayMultiSort(ByRef $aArray, $sSort = '0a', $iStartRow = 0, $iEndRow = 0) Local $iCol1, $Dir, $iLastRow, $iStart, $iRetV, $iEnd, $Err, $aSort2D[0][3] ;------------ $sSort (string) to $aSort2D (array) ----------- _ArrayAdd($aSort2D, Execute(StringTrimRight(StringRegExpReplace($sSort, "(?i)(c?)(\d+)([ad]?),?", "('$1'='c')*1&""|$2|""&('$3'='d')*1&@CRLF&"), 7))) _ArrayDisplay($aSort2D, "", "", 0, Default, "Case|Col#|Asc/Desc") ; ------------------------------------------------------------ If $iEndRow = 0 Or ($iEndRow > (UBound($aArray) - 1)) Then $iLastRow = UBound($aArray) - 1 $iStart = -1 $iEnd = -1 ; Sort the first column to be sorted. $iRetV = _ArraySortC($aArray, Number($aSort2D[0][2]), $iStartRow, $iLastRow, Number($aSort2D[0][1]), 0, Number($aSort2D[0][0])) ; Sort on the first column (stored in $aSort2D[0][1]) $Err = @error If $iRetV = 0 Then Return SetError($Err, 0, 0) ; Sort all remaining columns. For $j = 1 To UBound($aSort2D) - 1 ; For each group of equal values in the previous sorted column, sort the present column values on the same rows of each group. $iStart = -1 For $i = $iStartRow To $iLastRow Switch $i Case $iStartRow If $i <> $iLastRow Then If __TestExp($aArray, $aSort2D, $i, $j) Then $iStart = $i + 1 $iEnd = $i + 1 Else $iStart = $i $iEnd = $i + 1 EndIf EndIf Case $iLastRow $iEnd = $iLastRow If $iStart <> $iEnd Then $iRetV = _ArraySortC($aArray, Number($aSort2D[$j][2]), $iStart, $iEnd, Number($aSort2D[$j][1]), 0, Number($aSort2D[$j][0])) $Err = @error EndIf Case Else If __TestExp($aArray, $aSort2D, $i, $j) Then $iEnd = $i If $iStart <> $iEnd Then $iRetV = _ArraySortC($aArray, Number($aSort2D[$j][2]), $iStart, $iEnd, Number($aSort2D[$j][1]), 0, Number($aSort2D[$j][0])) $Err = @error EndIf $iStart = $i + 1 $iEnd = $iStart Else $iEnd = $i EndIf EndSwitch Next Next Return SetError($Err, 0, $iRetV) EndFunc ;==>_ArrayMultiSort ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __TestExp ; Description ...: Creates and tests a test expression. Internally used function by the _ArrayMultiSort() function ; Syntax.........: __TestExp($aA, $aC, $k, $m) ; Parameters ....: $aA - The 2D array to be sorted. ; $aC - A 2D array containing to column sorting order and each column's direction of sorting.(See Modified note) ; $k - The array's row index of the data being sorted. ; $m - The array's row index of the current column being sorted ; Return values .: 1 - When any of the created test expressions are true; or, ; 0 - When all of the created test expressions are false. ; Author ........: Malkey ; Modified.......: Malkey 2019/3/9 Array $aC is now [n][3] where [n][0] is case sensitivity; [n][1] is Column#; [n][2] is sort asc or desc. ; Remarks .......: This function is used internally by the _ArrayMultiSort function. The created expression tests the particular ; row, $k, of the test data, for each previously sorted column, $m -1, wheather the value in that column, $aA[$k][$aC[$w][1]], ; is not equal to the next value in that same column, $aA[$k + 1][$aC[$w][1]], where $aC[$w][1] contains the previously sorted column number. ; =============================================================================================================================== Func __TestExp(ByRef $aA, ByRef $aC, $k, $m) For $w = 0 To $m - 1 If ($aA[$k + 1][$aC[$w][1]]) <> ($aA[$k][$aC[$w][1]]) Then ; Like an "Or" join statements. If just one of the expressions in the For-Next loop is true, then function returns "1" (True). Return 1 EndIf Next Return 0 EndFunc ;==>__TestExp ; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ; =============================================================================================== ; The following three function, ; _ArraySortC, __ArrayQuickSort1DC, and __ArrayQuickSort2DC are modied copies of the functions, ; _ArraySort, __ArrayQuickSort1D, and __ArrayQuickSort2D from the Array.au3 include file. ; The modifications allow case sensitive sorting of arrays. ; =============================================================================================== ; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ; #FUNCTION# ==================================================================================================================== ; Author ........: Jos ; Modified.......: LazyCoder - added $iSubItem option; Tylo - implemented stable QuickSort algo; Jos - changed logic to correctly Sort arrays with mixed Values and Strings; Melba23 - implemented stable pivot algo; Malkey - added case sensitive sorting. ; =============================================================================================================================== Func _ArraySortC(ByRef $aArray, $iDescending = 0, $iStart = 0, $iEnd = 0, $iSubItem = 0, $iPivot = 0, $iCase = 0) If $iDescending = Default Then $iDescending = 0 If $iStart = Default Then $iStart = 0 If $iEnd = Default Then $iEnd = 0 If $iSubItem = Default Then $iSubItem = 0 If $iPivot = Default Then $iPivot = 0 If $iCase = Default Then $iCase = 0 If Not IsArray($aArray) Then Return SetError(1, 0, 0) Local $iUBound = UBound($aArray) - 1 If $iUBound = -1 Then Return SetError(5, 0, 0) ; Bounds checking If $iEnd = Default Then $iEnd = 0 If $iEnd < 1 Or $iEnd > $iUBound Or $iEnd = Default Then $iEnd = $iUBound If $iStart < 0 Or $iStart = Default Then $iStart = 0 If $iStart > $iEnd Then Return SetError(2, 0, 0) ; Sort Switch UBound($aArray, $UBOUND_DIMENSIONS) Case 1 If $iPivot Then ; Switch algorithms as required __ArrayDualPivotSort($aArray, $iStart, $iEnd) Else __ArrayQuickSort1DC($aArray, $iStart, $iEnd, $iCase) EndIf If $iDescending Then _ArrayReverse($aArray, $iStart, $iEnd) Case 2 If $iPivot Then Return SetError(6, 0, 0) ; Error if 2D array and $iPivot Local $iSubMax = UBound($aArray, 2) - 1 ; $UBOUND_COLUMNS (2) If $iSubItem > $iSubMax Then Return SetError(3, 0, 0) If $iDescending Then $iDescending = -1 Else $iDescending = 1 EndIf __ArrayQuickSort2DC($aArray, $iDescending, $iStart, $iEnd, $iSubItem, $iSubMax, $iCase) Case Else Return SetError(4, 0, 0) EndSwitch Return 1 EndFunc ;==>_ArraySortC ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __ArrayQuickSort1DC (Appended 'C' for case sensitivity) ; Description ...: Helper function for sorting 1D arrays ; Syntax.........: __ArrayQuickSort1D ( ByRef $aArray, ByRef $iStart, ByRef $iEnd ) ; Parameters ....: $aArray - Array to sort ; $iStart - Index of array to start sorting at ; $iEnd - Index of array to stop sorting at ; Return values .: None ; Author ........: Jos van der Zande, LazyCoder, Tylo, Ultima ; Modified.......: Malkey ; Remarks .......: For Internal Use Only. All StringCompare functions have $iCase parameter added for optional case sensitivity. ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __ArrayQuickSort1DC(ByRef $aArray, Const ByRef $iStart, Const ByRef $iEnd, Const ByRef $iCase) If $iEnd <= $iStart Then Return Local $vTmp ; InsertionSort (faster for smaller segments) If ($iEnd - $iStart) < 15 Then Local $vCur For $i = $iStart + 1 To $iEnd $vTmp = $aArray[$i] If IsNumber($vTmp) Then For $j = $i - 1 To $iStart Step -1 $vCur = $aArray[$j] ; If $vTmp >= $vCur Then ExitLoop If ($vTmp >= $vCur And IsNumber($vCur)) Or (Not IsNumber($vCur) And StringCompare($vTmp, $vCur, $iCase) >= 0) Then ExitLoop $aArray[$j + 1] = $vCur Next Else For $j = $i - 1 To $iStart Step -1 If (StringCompare($vTmp, $aArray[$j], $iCase) >= 0) Then ExitLoop $aArray[$j + 1] = $aArray[$j] Next EndIf $aArray[$j + 1] = $vTmp Next Return EndIf ; QuickSort Local $L = $iStart, $R = $iEnd, $vPivot = $aArray[Int(($iStart + $iEnd) / 2)], $bNum = IsNumber($vPivot) Do If $bNum Then ; While $aArray[$L] < $vPivot While ($aArray[$L] < $vPivot And IsNumber($aArray[$L])) Or (Not IsNumber($aArray[$L]) And StringCompare($aArray[$L], $vPivot, $iCase) < 0) $L += 1 WEnd ; While $aArray[$R] > $vPivot While ($aArray[$R] > $vPivot And IsNumber($aArray[$R])) Or (Not IsNumber($aArray[$R]) And StringCompare($aArray[$R], $vPivot, $iCase) > 0) $R -= 1 WEnd Else While (StringCompare($aArray[$L], $vPivot, $iCase) < 0) $L += 1 WEnd While (StringCompare($aArray[$R], $vPivot, $iCase) > 0) $R -= 1 WEnd EndIf ; Swap If $L <= $R Then $vTmp = $aArray[$L] $aArray[$L] = $aArray[$R] $aArray[$R] = $vTmp $L += 1 $R -= 1 EndIf Until $L > $R __ArrayQuickSort1DC($aArray, $iStart, $R, $iCase) __ArrayQuickSort1DC($aArray, $L, $iEnd, $iCase) EndFunc ;==>__ArrayQuickSort1DC ; #INTERNAL_USE_ONLY# =========================================================================================================== ; Name...........: __ArrayQuickSort2DC (Appended 'C' for case sensitivity) ; Description ...: Helper function for sorting 2D arrays ; Syntax.........: __ArrayQuickSort2D ( ByRef $aArray, ByRef $iStep, ByRef $iStart, ByRef $iEnd, ByRef $iSubItem, ByRef $iSubMax ) ; Parameters ....: $aArray - Array to sort ; $iStep - Step size (should be 1 to sort ascending, -1 to sort descending!) ; $iStart - Index of array to start sorting at ; $iEnd - Index of array to stop sorting at ; $iSubItem - Sub-index to sort on in 2D arrays ; $iSubMax - Maximum sub-index that array has ; Return values .: None ; Author ........: Jos van der Zande, LazyCoder, Tylo, Ultima ; Modified.......: Malkey ; Remarks .......: For Internal Use Only. All StringCompare functions have $iCase parameter added for optional case sensitivity. ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func __ArrayQuickSort2DC(ByRef $aArray, Const ByRef $iStep, Const ByRef $iStart, Const ByRef $iEnd, Const ByRef $iSubItem, Const ByRef $iSubMax, Const ByRef $iCase) If $iEnd <= $iStart Then Return ; QuickSort Local $vTmp, $L = $iStart, $R = $iEnd, $vPivot = $aArray[Int(($iStart + $iEnd) / 2)][$iSubItem], $bNum = IsNumber($vPivot) Do If $bNum Then ; While $aArray[$L][$iSubItem] < $vPivot While ($iStep * ($aArray[$L][$iSubItem] - $vPivot) < 0 And IsNumber($aArray[$L][$iSubItem])) Or (Not IsNumber($aArray[$L][$iSubItem]) And $iStep * StringCompare($aArray[$L][$iSubItem], $vPivot, $iCase) < 0) $L += 1 WEnd ; While $aArray[$R][$iSubItem] > $vPivot While ($iStep * ($aArray[$R][$iSubItem] - $vPivot) > 0 And IsNumber($aArray[$R][$iSubItem])) Or (Not IsNumber($aArray[$R][$iSubItem]) And $iStep * StringCompare($aArray[$R][$iSubItem], $vPivot, $iCase) > 0) $R -= 1 WEnd Else While ($iStep * StringCompare($aArray[$L][$iSubItem], $vPivot, $iCase) < 0) $L += 1 WEnd While ($iStep * StringCompare($aArray[$R][$iSubItem], $vPivot, $iCase) > 0) $R -= 1 WEnd EndIf ; Swap If $L <= $R Then For $i = 0 To $iSubMax $vTmp = $aArray[$L][$i] $aArray[$L][$i] = $aArray[$R][$i] $aArray[$R][$i] = $vTmp Next $L += 1 $R -= 1 EndIf Until $L > $R __ArrayQuickSort2DC($aArray, $iStep, $iStart, $R, $iSubItem, $iSubMax, $iCase) __ArrayQuickSort2DC($aArray, $iStep, $L, $iEnd, $iSubItem, $iSubMax, $iCase) EndFunc ;==>__ArrayQuickSort2DC Here is an example using the _ArrayMultiSort function.
      #include <ArrayMultiSortCase.au3> ;#cs ; --------------- Create test data -------------- Global $array[2001][5] = [["Col 0", "Col 1", "Col 2", "Col 3", "Col 4"]] ; For $i = 1 To 2000 $array[$i][0] = (Random(0, 1, 1) ? Chr(Random(97, 99, 1)) : Chr(Random(65, 67, 1))) $array[$i][1] = (Random(0, 1, 1) ? Chr(Random(97, 99, 1)) : Chr(Random(65, 67, 1))) $array[$i][2] = (Random(0, 1, 1) ? Int(Random(0, 2, 1) & Random(1, 3, 1)) : (Random(0, 1, 1) ? Chr(Random(97, 99, 1)) : Chr(Random(65, 67, 1)))) $array[$i][3] = Random(1, 5, 1) $array[$i][4] = Random(1, 5, 1) Next ; -----------> End of Create test data ----------- Local $aArray = $array ; Sort column 0(asc+case sensitive) then column 1(desc+case insensitive), then columns 2(asc+case sensitive), 3(desc+case insensitive), and 4(asc+case insensitive). _ArrayMultiSort($aArray, "c0a,1d,c2a,3d,4a", 1) _ArrayDisplay($aArray, "Sorted c0a,1d,c2a,3d,4a") Local $aArray = $array _ArrayMultiSort($aArray, "1d, c0, c2d, 3, 4d", 1) ; Sort column 1(desc) then column 0(asc+case sensitive), then columns 2(desc+case sensitive), 3(asc), and 4(desc). _ArrayDisplay($aArray, "Sorted 1d,c0a,c2d,3a,4d") ;#ce ;#cs Local $a_Test[18][2] = [["Color", "#"], ["Black", 3], ["Blue", 3], ["white", 3], ["Brown", 4], ["Clear", 3], ["Gold", 3], ["gold", 3], _ ["green", 3], ["Green", 3], ["brown", 4], ["Purple", 3], ["Red", 3], ["Royal", 3], ["Silver", 2], ["Sky", 2], ["White", 3], ["Yellow", 5]] _ArrayDisplay($a_Test, "Unsorted") _ArrayMultiSort($a_Test, "1d,0a", 1) ; Sort Column 1 descending, then Column 0 ascending _ArrayDisplay($a_Test, "Sorted 1d,0a Starting @ row1") _ArrayMultiSort($a_Test, "1d,c0a", 1) ; Sort Column 1 descending, then Column 0 ascending+case sensitive _ArrayDisplay($a_Test, "Sorted 1d,c0a Starting @ row1") ;#ce ;#cs Local $aTest[] = [2, "Black", "Blue", 3, "Brown", 4, "Clear", 3, "Gold", 3, "green", 13, "Gray", 3, "gold", 23, "Green", 3, "gray", 33, "Purple", 3, "Red", 3, "Royal", 3, "Silver", 2, "Sky", 2, "White", 3, "Yellow", 5] _ArrayMultiSort($aTest, 'c0a') _ArrayDisplay($aTest, "Sorted c0a") ;#ce  
      Here is an example to test the _ArraySortC function which is the same as _ArraySort function, but with an extra parameter "$iCase" added.
      #include <ArrayMultiSortCase.au3> Local $array[9][3] = [ _ ["age", 40, 8], _ [1, 32, 7], _ [4, 16, 9], _ ["ace", 32, 7], _ ["ass", 16, 9], _ [35, 32, 7], _ [24, 16, 9], _ ["Ama", 5, 0], _ ["Aft", 19, 6]] Local $avArray = $array ;_ArrayDisplay($avArray, "$avArray BEFORE _ArraySort()") _ArraySortC($avArray) ;, 0, 0, 0, 0, 0) _ArrayDisplay($avArray, "Asc col0") $avArray = $array _ArraySortC($avArray, 0, 0, 0, 0, 0, 1) _ArrayDisplay($avArray, "Asc col0; case-sensitive") $avArray = $array _ArraySortC($avArray, 1, 0, 0, 0, 0, 1) _ArrayDisplay($avArray, "Desc col0; case-sensitive") $avArray = $array _ArraySortC($avArray, 1, 1, 0, 0, 0, 1) _ArrayDisplay($avArray, "Start row1; Desc col0; case-sensitive")  
    • By TrashBoat
      Im trying to iterate through a 2d array created by 
      ProcessList() function that returns a 2d array containing names and pid's, i wanted to filter through the names and delete entries that have "svchost.exe" and this is how i did it
      Func _Sanitize($array) For $x = 1 To UBound($array) - 1 ConsoleWrite($array[$x][0] & @CRLF) If $array[$x][0] == "svchost.exe" Then _ArrayDelete($array, $x) EndIf Next EndFunc ;==>_Sanitize And i get this error message
      What is wrong?
    • By TheDcoder
      Hello Guys! I wanted to share all my knowledge on arrays! Hope may enjoy the article , Lets start!
      Declaring arrays!
      Declaring arrays is a little different than other variables: 
      ; Rules to follow while declaring arrays: ; ; Rule #1: You must have a declarative keyword like Dim/Global/Local before the declaration unless the array is assigned a value from a functions return (Ex: StringSplit) ; Rule #2: You must declare the number of dimensions but not necessarily the size of the dimension if you are gonna assign the values at the time of declaration. #include <Array.au3> Local $aEmptyArray[0] ; Creates an Array with 0 elements (aka an Empty Array). Local $aArrayWithData[1] = ["Data"] _ArrayDisplay($aEmptyArray) _ArrayDisplay($aArrayWithData) That's it
      Resizing Arrays
      Its easy! Just like declaring an empty array! ReDim is our friend here:
      #include <Array.au3> Local $aArrayWithData[1] = ["Data1"] ReDim $aArrayWithData[2] ; Change the number of elements in the array, I have added an extra element! $aArrayWithData[1] = "Data2" _ArrayDisplay($aArrayWithData) Just make sure that you don't use ReDim too often (especially don't use it in loops!), it can slow down you program.
      Best practice of using "Enum"
      You might be wondering what they might be... Do you know the Const keyword which you use after Global/Local keyword? Global/Local are declarative keywords which are used to declare variables, of course, you would know that already by now , If you check the documentation for Global/Local there is a optional parameter called Const which willl allow you to "create a constant rather than a variable"... Enum is similar to Const, it declares Integers (ONLY Integers):
      Global Enum $ZERO, $ONE, $TWO, $THREE, $FOUR, $FIVE, $SIX, $SEVEN, $EIGHT, $NINE ; And so on... ; $ZERO will evaluate to 0 ; $ONE will evaluate to 1 ; You get the idea :P ; Enum is very useful to declare Constants each containing a number (starting from 0) This script will demonstrate the usefulness and neatness of Enums :
      ; We will create an array which will contain details of the OS Global Enum $ARCH, $TYPE, $LANG, $VERSION, $BUILD, $SERVICE_PACK Global $aOS[6] = [@OSArch, @OSType, @OSLang, @OSVersion, @OSBuild, @OSServicePack] ; Now, if you want to access anything related to the OS, you would do this: ConsoleWrite(@CRLF) ConsoleWrite('+>' & "Architecture: " & $aOS[$ARCH] & @CRLF) ConsoleWrite('+>' & "Type: " & $aOS[$TYPE] & @CRLF) ConsoleWrite('+>' & "Langauge: " & $aOS[$LANG] & @CRLF) ConsoleWrite('+>' & "Version: " & $aOS[$VERSION] & @CRLF) ConsoleWrite('+>' & "Build: " & $aOS[$BUILD] & @CRLF) ConsoleWrite('+>' & "Service Pack: " & $aOS[$SERVICE_PACK] & @CRLF) ConsoleWrite(@CRLF) ; Isn't it cool? XD You can use this in your UDF(s) or Program(s), it will look very neat!
      Looping through an Array
      Looping through an array is very easy! . There are 2 ways to loop an array in AutoIt!
      Simple Way:
      ; This is a very basic way to loop through an array ; In this way we use a For...In...Next Loop! Global $aArray[2] = ["Foo", "Bar"] ; Create an array ; This loop will loop 2 times because our $aArray contains 2 elements. For $vElement In $aArray ; $vElement will contain the value of the elements in the $aArray... one element at a time. ConsoleWrite($vElement & @CRLF) ; Prints the element out to the console Next ; And that's it! Advanced Way:
      ; This is an advanced way to loop through an array ; In this way we use a For...To...Next Loop! Global $aArray[4] = ["Foo", "Bar", "Baz", "Quack"] ; Create an array ; This loop will loop 2 times because our $aArray contains 2 elements. For $i = 0 To UBound($aArray) - 1 ; $i is automatically created and is set to zero, UBound($aArray) returns the no. of elements in the $aArray. ConsoleWrite($aArray[$i] & @CRLF) ; Prints the element out to the console. Next ; This is the advanced way, we use $i to access the elements! ; With the advanced method you can also use the Step keyword to increase the offset in each "step" of the loop: ; This will only print every 2nd element starting from 0 ConsoleWrite(@CRLF & "Every 2nd element: " & @CRLF) For $i = 0 To UBound($aArray) - 1 Step 2 ConsoleWrite($aArray[$i] & @CRLF) Next ; This will print the elements in reverse order! ConsoleWrite(@CRLF & "In reverse: " & @CRLF) For $i = UBound($aArray) - 1 To 0 Step -1 ConsoleWrite($aArray[$i] & @CRLF) Next ; And that ends this section! For some reason, many people use the advance way more than the simple way . For more examples of loops see this post by @FrancescoDiMuro!
      Interpreting Multi-Dimensional Arrays
      Yeah, its the most brain squeezing problem for newbies, Imagining an 3D Array... I will explain it in a very simple way for ya, so stop straining you brain now! . This way will work for any array regardless of its dimensions...
      Ok, Lets start... You can imagine an array as a (data) mine of information:

      ; Note that: ; Dimension = Level (except the ground level :P) ; Element in a Dimension = Path ; Level 2 ----------\ ; Level 1 -------\ | ; Level 0 ----\ | | ; v v v Local $aArray[2][2][2] ; \-----/ ; | ; v ; Ground Level ; As you can see that $aArray is the Ground Level ; All the elements start after the ground level, i.e from level 0 ; Level 0 Contains 2 different paths ; Level 1 Contains 4 different paths ; Level 2 Contains 8 different paths ; When you want too fill some data in the data mine, ; You can do that like this: $aArray[0][0][0] = 1 $aArray[0][0][1] = 2 $aArray[0][1][0] = 3 $aArray[0][1][1] = 4 $aArray[1][0][0] = 5 $aArray[1][0][1] = 6 $aArray[1][1][0] = 7 $aArray[1][1][1] = 8 ; Don't get confused with the 0s & 1s, Its just tracing the path! ; Try to trace the path of a number with the help of the image! Its super easy! :D I hope you might have understand how an array looks, Mapping your way through is the key in Multi-Dimensional arrays, You take the help of notepad if you want! Don't be shy!
      Frequently Asked Questions (FAQs) & Their answers
      Q #1. What are Arrays?
      A. An Array is an datatype of an variable (AutoIt has many datatypes of variables like "strings", "integers" etc. Array is one of them). An Array can store information in a orderly manner. An Array consist of elements, each element can be considered as a variable (and yes, each element has its own datatype!). AutoIt can handle 16,777,216 elements in an Array, If you have an Array with 16,777,217 elements then AutoIt crashes.
      Q #2. Help! I get an error while declaring an Array!?
      A. You tried to declare an array like this:
      $aArray[1] = ["Data"] That is not the right way, Array is a special datatype, since its elements can be considered as individual variables you must have an declarative keyword like Dim/Global/Local before the declaration, So this would work:
      Local $aArray[1] = ["Data"] Q #3. How can I calculate the no. of elements in an array?
      A. The UBound function is your answer, Its what exactly does! If you have an multi-dimensional Array you can calculate the total no. of elements in that dimension by specifying the dimension in the second parameter of UBound
      Q #4. Why is my For...Next loop throwing an error while processing an Array?
      A.  You might have done something like this:
      #include <MsgBoxConstants.au3> Local $aArray[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Local $iMyNumber = 0 For $i = 0 To UBound($aArray) ; Concentrate here! $iMyNumber += $aArray[$i] Next MsgBox($MB_OK, "Sum of all Numbers!", $iMyNumber) Did you notice the mistake? UBound returns the no. of elements in an array with the index starting from 1! That's right, you need to remove 1 from the total no. of elements in order to process the array because the index of an array starts with 0! So append a simple - 1 to the statment:
      #include <MsgBoxConstants.au3> Local $aArray[10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Local $iMyNumber = 0 For $i = 0 To UBound($aArray) - 1 $iMyNumber += $aArray[$i] Next MsgBox($MB_OK, "Sum of all Numbers!", $iMyNumber) Q #5. Can an Array contain an Array? How do I access an Array within an Array?
      A. Yes! It is possible that an Array can contain another Array! Here is an example of an Array within an Array:
      ; An Array can contain another Array in one of its elements ; Let me show you an example of what I mean ;) #include <Array.au3> Global $aArray[2] $aArray[0] = "Foo" Global $aChildArray[1] = ["Bar"] $aArray[1] = $aChildArray _ArrayDisplay($aArray) ; Did you see that!? The 2nd element is an {Array} :O ; But how do we access it??? ; You almost guessed it, like this: ; Just envolope the element which contains the {Array} (as shown in _ArrayDisplay) with brackets (or parentheses)! :D ConsoleWrite(($aArray[1])[0]) ; NOTE the brackets () around $aArray[1]!!! They are required or you would get an syntax error! ; So this: $aArray[1][0] wont work!  
      More FAQs coming soon!
    • By jasty
      In every other language doing   
      dim $a[3] = [1, 2, 3] $b = $a creates a reference to a but in autoit it always makes a copy.  I didn't realize this until I have performance problems and now I'm trying to switch over to the reference behavior.  How can I change the above statement so b is a reference to a and not a full copy? 
    • By rm4453
      Hello,
       
      I am currently writing a program that parses a massive table from a website, and need a way to add a progress bar while parsing.
      I am currently using the function _IETableWriteToArray($oObj, True) to parse the array. I need the progress bar to update as the table is parsed, not just at the end of the parsing.
      Any help at all would be very much appreciated!
       
      *EDIT --> The array I am left with after parsing is $array[0-50000][16]
×
×
  • Create New...