Snippets ( AutoIt Array ): Difference between revisions

From AutoIt Wiki
Jump to navigation Jump to search
m (+Snippets Category (+ normalize top))
 
(11 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__TOC__[[category:Snippets]]{{Snippet Credit Header}}
__TOC__
===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _1Dto2DArray() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====


<syntaxhighlight lang="autoit">
[[category:Snippets]]
 
{{Snippet Credit Header}}
 
== _1Dto2DArray ==
 
{{Snippet Header
|AuthorURL=35302-guinness
|AuthorName=guinness
}}
 
<syntaxhighlight lang = "autoit">
#include <Array.au3>
#include <Array.au3>


Line 10: Line 20:
_ArrayDisplay($aArray_1D)
_ArrayDisplay($aArray_1D)


Global $aArray_1D[10] = ["Data", "Data","Data", "Data","Data", "Data","Data", "Data","Data", "Data"]
Global $aArray_1D[10] = ["Data", "Data", "Data", "Data", "Data", "Data", "Data", "Data", "Data", "Data"]
_ArrayDisplay($aArray_1D)
_ArrayDisplay($aArray_1D)
_1Dto2DArray($aArray_1D, 9)
_1Dto2DArray($aArray_1D, 9)
_ArrayDisplay($aArray_1D)
_ArrayDisplay($aArray_1D)


Func _1Dto2DArray(ByRef $aArray, Const $iAdditionalColumns = 1)
    Local Const $iSize = UBound($aArray, 1)


Func _1Dto2DArray(ByRef $aArray, $iAdditionalColumns = 1)
    Local $iSize = UBound($aArray, 1)
     Local $aReturn[$iSize][$iAdditionalColumns + 1]
     Local $aReturn[$iSize][$iAdditionalColumns + 1]
     For $A = 0 To $iSize - 1
     For $A = 0 To $iSize - 1
         $aReturn[$A][0] = $aArray[$A]
         $aReturn[$A][0] = $aArray[$A]
     Next
     Next
     $aArray = $aReturn
     $aArray = $aReturn
     Return $aReturn
     Return $aReturn
EndFunc  ;==>_1Dto2DArray
EndFunc  ;==>_1Dto2DArray
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _ArrayAddEx() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
<syntaxhighlight lang="autoit">
 
== _ArrayAddEx ==
 
{{Snippet Header
|AuthorURL=35302-guinness
|AuthorName=guinness
}}
 
<syntaxhighlight lang = "autoit">
#include <Array.au3>
#include <Array.au3>


Line 81: Line 102:
EndFunc  ;==>_ArrayAddEx
EndFunc  ;==>_ArrayAddEx
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _ArrayEmpty() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]


<syntaxhighlight lang="autoit">
== _ArrayEmpty ==
; Creates Empty 1D & 2D Array's
 
{{Snippet Header
|AuthorURL=35302-guinness
|AuthorName=guinness
|Desc=Creates Empty 1D & 2D Array's
}}


<syntaxhighlight lang = "autoit">
#include <Array.au3>
#include <Array.au3>


Local $aMain[11] = [10]
Global $aMain[11] = [10]
_ArrayEmpty($aMain)
_ArrayEmpty($aMain)
_ArrayDisplay($aMain, "Empty 1D Array.")
_ArrayDisplay($aMain, "Empty 1D Array.")


Local $aMainEx[11][3] = [[10]]
Global $aMainEx[11][3] = [[10]]
_ArrayEmpty($aMainEx)
_ArrayEmpty($aMainEx)
_ArrayDisplay($aMainEx, "Empty 2D Array.")
_ArrayDisplay($aMainEx, "Empty 2D Array.")
Line 105: Line 131:


         Case 2
         Case 2
             Local $aEmpty2D[1][UBound($aArray, 2)]
             ; Edit by DatMCEyeBall - Instead of emptying the array to [1][$x] we empty it to [$x][$y]
            ; where $x is the 1st dimensions size and $y the 2nd.
            ; Previous code was: Local $aEmpty2D[1][UBound($aArray, 2)]
            ; This way we preserve the arrays' original size.
            Local $aEmpty2D[UBound($aArray, 1)][UBound($aArray, 2)]
             $aArray = $aEmpty2D
             $aArray = $aEmpty2D
     EndSwitch
     EndSwitch
Line 111: Line 141:
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _ArrayGet() & _ArraySet() ~ Author - [http://www.autoitscript.com/forum/user/31965-progandy/ ProgAndy] '''</blockquote> =====
[[#top | ReturnToContents]]


<syntaxhighlight lang="autoit">
== _ArrayGet ==
; Easy access for nested arrays up to 20 nested dimensions (including multidimensional arrays)
 
Local $b[2] = ["a", "b"]
{{Snippet Header
Local $a[3][2] = [[0, -1], [$b, "x"], [1, -2]]
|AuthorURL=31965-progandy
|AuthorName=ProgAndy
|Desc=Easy access for nested arrays up to 20 nested dimensions (including multidimensional arrays)
}}
 
<syntaxhighlight lang = "autoit">
Global $b[2] = ["a", "b"]
Global $a[3][2] = [[0, -1], [$b, "x"], [1, -2]]


Local $c[5] = [11, 22, 33, 44, 55]
Global $c[5] = [11, 22, 33, 44, 55]


MsgBox(0, "", _ArrayGet($a, 1, 0, 1) & " # " & @error)
MsgBox(0, "", _ArrayGet($a, 1, 0, 1) & " # " & @error)
_ArraySet($a, $c, 1, 0, 0)
MsgBox(0, @error, @extended)
MsgBox(0, "", _ArrayGet($a, 1, 0, 0, 3))


; #FUNCTION# ====================================================================================================================
; #FUNCTION# ====================================================================================================================
Line 166: Line 200:
; ===============================================================================================================================
; ===============================================================================================================================
Func _ArrayGet(ByRef $aArray, $i1, $i2=0, $i3=0, $i4=0, $i5=0, $i6=0, $i7=0, $i8=0, $i9=0, $i10=0, $i11=0, $i12=0, $i13=0, $i14=0, $i15=0, $i16=0, $i17=0, $i18=0, $i19=0, $i20=0)
Func _ArrayGet(ByRef $aArray, $i1, $i2=0, $i3=0, $i4=0, $i5=0, $i6=0, $i7=0, $i8=0, $i9=0, $i10=0, $i11=0, $i12=0, $i13=0, $i14=0, $i15=0, $i16=0, $i17=0, $i18=0, $i19=0, $i20=0)
      #forceref $i1, $i2, $i3, $i4, $i5, $i6, $i7, $i8, $i9, $i10, $i11, $i12, $i13, $i14, $i15, $i16, $i17, $i18, $i19, $i20
#forceref $i1, $i2, $i3, $i4, $i5, $i6, $i7, $i8, $i9, $i10, $i11, $i12, $i13, $i14, $i15, $i16, $i17, $i18, $i19, $i20
   ; ProgAndy
   ; ProgAndy
     Local $NULL
     Local $NULL
Line 189: Line 223:
     EndIf
     EndIf
EndFunc
EndFunc
</syntaxhighlight>
[[#top | ReturnToContents]]
== _ArraySet ==
{{Snippet Header
|AuthorURL=31965-progandy
|AuthorName=ProgAndy
|Desc=Easy access for nested arrays up to 20 nested dimensions (including multidimensional arrays)
}}
<syntaxhighlight lang = "autoit">
Global $b[2] = ['a', 'b'
Global $a[3][2] = [[0, -1], [$b, "x"], [1, -2]]


Global $c[5] = [11, 22, 33, 44, 55]


_ArraySet($a, $c, 1, 0, 0)


; #FUNCTION# ====================================================================================================================
; #FUNCTION# ====================================================================================================================
Line 235: Line 286:
Func _ArraySet(ByRef $aArray, Const $vValue, $i1, $i2=0, $i3=0, $i4=0, $i5=0, $i6=0, $i7=0, $i8=0, $i9=0, $i10=0, $i11=0, $i12=0, $i13=0, $i14=0, $i15=0, $i16=0, $i17=0, $i18=0, $i19=0, $i20=0)
Func _ArraySet(ByRef $aArray, Const $vValue, $i1, $i2=0, $i3=0, $i4=0, $i5=0, $i6=0, $i7=0, $i8=0, $i9=0, $i10=0, $i11=0, $i12=0, $i13=0, $i14=0, $i15=0, $i16=0, $i17=0, $i18=0, $i19=0, $i20=0)
     ; ProgAndy
     ; ProgAndy
     Local $NULL = False
     Local Const $NULL = False
 
     If Not IsArray($aArray) Then Return SetError(1, 1, $NULL)
     If Not IsArray($aArray) Then Return SetError(1, 1, $NULL)
     Local $iDims = UBound($aArray, 0)
     Local $iDims = UBound($aArray, 0)
     If @NumParams-2 < $iDims Then Return SetError(2, @NumParams-1, $NULL)
     If @NumParams-2 < $iDims Then Return SetError(2, @NumParams-1, $NULL)
     Local $sAccess = "$aArray"
     Local $sAccess = "$aArray"
     For $i = 1 To $iDims
     For $i = 1 To $iDims
         Local $iIndex = Int(Eval("i" & $i))
         Local $iIndex = Int(Eval("i" & $i))
Line 245: Line 301:
         $sAccess &= '[' & $iIndex & ']'
         $sAccess &= '[' & $iIndex & ']'
     Next
     Next
     If @NumParams-2 > $iDims Then
     If @NumParams-2 > $iDims Then
         $sAccess &= ", $vValue"
         $sAccess &= ", $vValue"
Line 254: Line 311:
         Return $fSuccess
         Return $fSuccess
     EndIf
     EndIf
     Switch $iDims
     Switch $iDims
         Case 1
         Case 1
Line 295: Line 353:
         Case 20
         Case 20
             $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12][$i13][$i14][$i15][$i16][$i17][$i18][$i19][$i20] = $vValue
             $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12][$i13][$i14][$i15][$i16][$i17][$i18][$i19][$i20] = $vValue
    EndSwitch
EndSwitch
 
     Return True
     Return True
EndFunc
EndFunc
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _ArrayLength() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _ArrayLength ==
 
{{Snippet Header
|AuthorURL=35302-guinness
|AuthorName=guinness
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang = "autoit">
Local $aArray[2][50]
Global $aArray[2][50]
ConsoleWrite(_ArrayLength($aArray, 2) & @CRLF) ;= > 10 - Total items
ConsoleWrite(_ArrayLength($aArray, 2) & @CRLF) ;= > 10 - Total items
ConsoleWrite(_ArrayLength($aArray, 1) & @CRLF) ;= > 5 - Columns
ConsoleWrite(_ArrayLength($aArray, 1) & @CRLF) ;= > 5 - Columns
Line 310: Line 376:
ConsoleWrite(@CRLF)
ConsoleWrite(@CRLF)


Local $aArray[2]
Global $aArray[2]
ConsoleWrite(_ArrayLength($aArray, 2) & @CRLF) ;= > 2 - Total items
ConsoleWrite(_ArrayLength($aArray, 2) & @CRLF) ;= > 2 - Total items
ConsoleWrite(_ArrayLength($aArray, 1) & @CRLF) ;= > 1 - Columns
ConsoleWrite(_ArrayLength($aArray, 1) & @CRLF) ;= > 1 - Columns
Line 317: Line 383:
Func _ArrayLength($aArray, $iType = 1)
Func _ArrayLength($aArray, $iType = 1)
     Local $iDim = UBound($aArray, 0)
     Local $iDim = UBound($aArray, 0)
     If $iDim < 0 Or $iDim > 2 Then ; If not a 1D or 2D Array, then return -1 and set @error to non-zero.
     If $iDim < 0 Or $iDim > 2 Then ; If not a 1D or 2D Array, then return -1 and set @error to non-zero.
         Return SetError(1, 0, -1)
         Return SetError(1, 0, -1)
     EndIf
     EndIf
     Switch $iType
     Switch $iType
         Case 2
         Case 2
Line 337: Line 405:
         Case Else
         Case Else
             Return UBound($aArray, 1)
             Return UBound($aArray, 1)
     EndSwitch
     EndSwitch
EndFunc  ;==>_ArrayLength
EndFunc  ;==>_ArrayLength
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _ArraySortNum() ~ Author - [http://www.autoitscript.com/forum/user/4813-smoke-n/ SmOke_N] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _ArraySortNum ==


<syntaxhighlight lang="autoit">
{{Snippet Header
|AuthorURL=4813-smoke-n
|AuthorName=SmOke_N
}}
 
<syntaxhighlight lang = "autoit">
; ArraySortNum(ByRef $nArray, $Ascending = 0, $Start = 1)
; ArraySortNum(ByRef $nArray, $Ascending = 0, $Start = 1)
; Sorts arrays based on TRUE Numerical Order, including Decimals
; Sorts arrays based on TRUE Numerical Order, including Decimals
Line 351: Line 425:


; Example sort Array created with StringSplit.
; Example sort Array created with StringSplit.
Local $avArray = StringSplit("6,8,10,4,2,13,12,129,106.1,106.4", ",")
Global $avArray = StringSplit("6,8,10,4,2,13,12,129,106.1,106.4", ",")


_ArraySortNum($avArray, 1)
_ArraySortNum($avArray, 1)
Line 359: Line 433:
For $i = $Start To UBound($nArray) - 2
For $i = $Start To UBound($nArray) - 2
Local $SE = $i
Local $SE = $i
If $Ascending = 0 Then
If $Ascending = 0 Then
For $x = $i To UBound($nArray) - 1
For $x = $i To UBound($nArray) - 1
Line 368: Line 443:
Next
Next
EndIf
EndIf
Local $HLD = $nArray[$i]
Local $HLD = $nArray[$i]
$nArray[$i] = $nArray[$SE]
$nArray[$i] = $nArray[$SE]
$nArray[$SE] = $HLD
$nArray[$SE] = $HLD
Next
Next
Line 375: Line 453:
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _AssocArray() ~ Author - [http://www.autoitscript.com/forum/user/31149-milesahead/ MilesAhead] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _AssocArray ==


<syntaxhighlight lang="autoit">
{{Snippet Header
|AuthorURL=31149-milesahead
|AuthorName=MilesAhead
}}
 
<syntaxhighlight lang = "autoit">
; use Scripting.Dictionary object for simple associative arrays
; use Scripting.Dictionary object for simple associative arrays
; with string keys. Key comparisons are case insensitive.
; with string keys. Key comparisons are case insensitive.
Line 401: Line 486:
Func _AssocArray()
Func _AssocArray()
     Local $aArray = ObjCreate("Scripting.Dictionary")
     Local $aArray = ObjCreate("Scripting.Dictionary")
     If @error Then
     If @error Then
         Return SetError(1, 0, 0)
         Return SetError(1, 0, 0)
     EndIf
     EndIf
     $aArray.CompareMode = 1
     $aArray.CompareMode = 1
     Return $aArray
     Return $aArray
EndFunc  ;==>_AssocArray
EndFunc  ;==>_AssocArray
Line 418: Line 506:
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _FuncListToArray() ~ Author - [http://www.autoitscript.com/forum/user/52-geosoft/ GEOSoft] '''</blockquote> =====
[[#top | ReturnToContents]]
 
== _DynEnumArray ==
 
{{Snippet Header
| AuthorURL = 26215-spudw2k
| AuthorName = spudw2k
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang="autoit">
Func _DynEnumArray($aArrayToEnum)
    Local $aDims[1][2]=[["ElementIdx","ElementCount"]]  ;Create an Array to Track Dimensions and Elements
    For $iX = 1 to UBound($aArrayToEnum,0)
        ReDim $aDims[$iX+1][2]
        $aDims[$iX][0]=0
        $aDims[$iX][1]=UBound($aArrayToEnum,$iX)
    Next
    Do  ;Loop Through Array Elements
        $sArray = "$aArrayToEnum"
        For $iX = 1 to UBound($aDims)-1
            $sArray &= "[" & $aDims[$iX][0] & "]"
        Next
        ConsoleWrite($sArray & " " & Execute($sArray) & @CRLF)  ;Output Subscript and Value
        $aDims[UBound($aDims)-1][0] += 1  ;Increment Last Dimension Element
        For $iY = UBound($aDims)-2 To 1 Step -1  ;Increment Dimension Element
            If $aDims[$iY+1][0] = $aDims[$iY+1][1] Then
                $aDims[$iY+1][0]=0
                $aDims[$iY][0]+=1
            EndIf
        Next
    Until $aDims[1][0]=$aDims[1][1]
    Return ConsoleWrite(@CRLF)
EndFunc
;Demos
Local $aArray[5]=["Never","Gonna","Give","You","Up"]
_DynEnumArray($aArray)
Local $aArray[6][2]=[["Penn", "Teller"],["Gilbert", "Sullivan"],["Sonny", "Cher"],["Stone", "Parker"],["Fischbacher", "Horn"],["Plant", "Page"]]
_DynEnumArray($aArray)
Local $aArray[2][3][2]=[[["All","Your"],["Base","Are"],["Belong","To Us"]],[["Is","This"],["The","Way"],["Back","Home"]]]
_DynEnumArray($aArray)
</syntaxhighlight>
[[#top | ReturnToContents]]
== _FuncListToArray ==
{{Snippet Header
|AuthorURL=52-geosoft
|AuthorName=GEOSoft
}}
<syntaxhighlight lang = "autoit">
$aFlist = _FuncListToArray(@ScriptFullPath)
$aFlist = _FuncListToArray(@ScriptFullPath)
If NOT @Error Then
If NOT @Error Then
Line 437: Line 579:
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' Identify Duplicate Values In Arrays ~ Author - [http://www.autoitscript.com/forum/user/1967-garyfrost/ GaryFrost] '''</blockquote> =====
[[#top | ReturnToContents]]


<syntaxhighlight lang="autoit">
== Identify Duplicate Values In Arrays ==
 
{{Snippet Header
|AuthorURL=1967-garyfrost
|AuthorName=GaryFrost
}}
 
<syntaxhighlight lang = "autoit">
; Identify Duplicate Values In Arrays
; Identify Duplicate Values In Arrays
#include <Array.au3>
#include <Array.au3>
Line 449: Line 598:
Func _SearchForDupes($a_arrays, $i_ReturnType = 0)
Func _SearchForDupes($a_arrays, $i_ReturnType = 0)
Local $Dupes[1] = [0]
Local $Dupes[1] = [0]
If $a_arrays[0] < 2 Then Return ''
If $a_arrays[0] < 2 Then Return ''
For $i = 1 To $a_a[0] - 1
For $i = 1 To $a_a[0] - 1
For $j = $i + 1 To $a_a[0]
For $j = $i + 1 To $a_a[0]
Line 455: Line 606:
Next
Next
Next
Next
If $Dupes[0] = 0 Then Return ''
If $Dupes[0] = 0 Then Return ''
_ArraySort($Dupes)
_ArraySort($Dupes)
If Not $i_ReturnType Then
If Not $i_ReturnType Then
Local $s_return = ''
Local $s_return = ''
For $x = 1 To $Dupes[0]
For $x = 1 To $Dupes[0]
$s_return &= $Dupes[$x] & ','
$s_return &= $Dupes[$x] & ','
Line 466: Line 621:
Return $Dupes
Return $Dupes
EndIf
EndIf
EndFunc  ;==>_SearchForDupes
EndFunc  ;==>_SearchForDupes


Func _FindDupe(ByRef $a_array01, ByRef $a_array02, ByRef $Dupes)
Func _FindDupe(ByRef $a_array01, ByRef $a_array02, ByRef $Dupes)
Local $found = False
Local $found = False
For $i = 0 To UBound($a_array01) - 1
For $i = 0 To UBound($a_array01) - 1
For $j = 0 To UBound($a_array02) - 1
For $j = 0 To UBound($a_array02) - 1
If $a_array01[$i] = $a_array02[$j] Then
If $a_array01[$i] = $a_array02[$j] Then
$found = False
$found = False
For $x = 1 To $Dupes[0]
For $x = 1 To $Dupes[0]
If $Dupes[$x] = $a_array01[$i] Then
If $Dupes[$x] = $a_array01[$i] Then
Line 481: Line 637:
EndIf
EndIf
Next
Next
If Not $found Then
If Not $found Then
$Dupes[0] += 1
$Dupes[0] += 1
Line 492: Line 649:
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _Is1DArray() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
<syntaxhighlight lang="autoit">
 
Local $sString = 'String'
== _Is1DArray ==
 
{{Snippet Header
|AuthorURL=35302-guinness
|AuthorName=guinness
}}
 
<syntaxhighlight lang = "autoit">Local $sString = 'String'
Local $a1DArray[2] = [1, 'Row_1']
Local $a1DArray[2] = [1, 'Row_1']
Local $a2DArray[2][2] = [[1],['Row_1: Col_1', 'Row_1: Col_2']]
Local $a2DArray[2][2] = [[1],['Row_1: Col_1', 'Row_1: Col_2']]
Line 503: Line 667:


; Check if a variable is a 1D Array.
; Check if a variable is a 1D Array.
Func _Is1DArray(ByRef $aArray)
Func _Is1DArray(ByRef Const $aArray)
    Return IsArray($aArray) And (UBound($aArray, 0) = 1)
Return UBound($aArray) And (UBound($aArray, 0) = 1)
EndFunc  ;==>_Is1DArray
EndFunc  ;==>_Is1DArray
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _Is2DArray() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
<syntaxhighlight lang="autoit">
 
Local $sString = 'String'
== _Is2DArray ==
 
{{Snippet Header
|AuthorURL=35302-guinness
|AuthorName=guinness
|Desc=Check if a variable is a 2D Array.
}}
 
<syntaxhighlight lang = "autoit">Local $sString = 'String'
Local $a1DArray[2] = [1, 'Row_1']
Local $a1DArray[2] = [1, 'Row_1']
Local $a2DArray[2][2] = [[1],['Row_1: Col_1', 'Row_1: Col_2']]
Local $a2DArray[2][2] = [[1],['Row_1: Col_1', 'Row_1: Col_2']]
Line 520: Line 691:


; Check if a variable is a 2D Array.
; Check if a variable is a 2D Array.
Func _Is2DArray(ByRef $aArray)
Func _Is2DArray(ByRef Const $aArray)
    Return IsArray($aArray) And (UBound($aArray, 0) = 2)
Return UBound($aArray) And (UBound($aArray, 0) = 2)
EndFunc  ;==>_Is2DArray
EndFunc  ;==>_Is2DArray
</syntaxhighlight>
</syntaxhighlight>
[[#top|Return To Contents]]


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _IsInBounds() ~ Author - [http://www.autoitscript.com/forum/user/35302-guinness/ guinness] '''</blockquote> =====
[[#top | ReturnToContents]]
<syntaxhighlight lang="autoit">
 
== _IsInBounds ==
 
{{Snippet Header
|AuthorURL=35302-guinness
|AuthorName=guinness
}}
 
<syntaxhighlight lang = "autoit">#include <Constants.au3>
 
Local $aArray[5] = ['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']
Local $aArray[5] = ['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']


Line 535: Line 714:


If _IsInBounds($aArray, 4) Then
If _IsInBounds($aArray, 4) Then
    MsgBox(4096, '', 'Position 4 contains the value: "' & $aArray[4] & '"')
MsgBox($MB_SYSTEMMODAL, '', 'Position 4 contains the value: "' & $aArray[4] & '"')
EndIf
EndIf


Func _IsInBounds(ByRef $aArray, $iIndex)
Func _IsInBounds(ByRef Const $aArray, $iIndex)
    Local $iDim = UBound($aArray, 0)
Local $iDim = UBound($aArray, 0)
    If $iDim < 0 Or $iDim > 2 Then ; If not a 1D or 2D Array, then return -1 and set @error to non-zero.
If $iDim < 0 Or $iDim > 2 Then ; If not a 1D or 2D Array, then return -1 and set @error to non-zero.
        Return SetError(1, 0, -1)
Return SetError(1, 0, False)
    EndIf
EndIf
    Return $iIndex < UBound($aArray, 1)
Return $iIndex < UBound($aArray, 1)
EndFunc  ;==>_IsInBounds
EndFunc  ;==>_IsInBounds</syntaxhighlight>
</syntaxhighlight>
 
[[#top|Return To Contents]]
[[#top | ReturnToContents]]
 
== _MultiFileListToArray ==


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' _MultiFileListToArray() ~ Author - [http://www.autoitscript.com/forum/user/4920-valuater/ Valuater] '''</blockquote> =====
{{Snippet Header
|AuthorURL=4920-valuater
|AuthorName=Valuater
}}


<syntaxhighlight lang="autoit">
<syntaxhighlight lang = "autoit">
; Multiple File List to Array
; Multiple File List to Array


Line 556: Line 740:


; for display only
; for display only
$GUI = GUICreate(' _MultiFileListToArray - DEMO')
$GUI = GUICreate(" _MultiFileListToArray - DEMO")
$Edit1 = GUICtrlCreateEdit("", 50, 50, 300, 300)
$Edit1 = GUICtrlCreateEdit("", 50, 50, 300, 300)
For $x = 1 To UBound($Files_List) - 1
For $x = 1 To UBound($Files_List) - 1
Line 597: Line 781:
</syntaxhighlight>
</syntaxhighlight>


===== <blockquote style="background-color:white; padding:1em; border:2px solid #8FBC8F">''' Return Min Or Max Number From 1 Or 2 Arrays ~ Author - [http://www.autoitscript.com/forum/user/1967-garyfrost/ GaryFrost] '''</blockquote> =====
[[#top | ReturnToContents]]


<syntaxhighlight lang="autoit">
== Return Min or Max Number From One or Two Arrays ==
; Return Min Or Max Number From 1 Or 2 Arrays
 
{{Snippet Header
|AuthorURL=1967-garyfrost
|AuthorName=GaryFrost
}}


<syntaxhighlight lang = "autoit">
#include <Array.au3>
#include <Array.au3>


Line 609: Line 798:
       Return (0)
       Return (0)
   EndIf
   EndIf
   If @NumParams = 3 Then
   If @NumParams = 3 Then
       If Not IsArray($a_nums02) Then
       If Not IsArray($a_nums02) Then
Line 615: Line 805:
       EndIf
       EndIf
   EndIf
   EndIf
;
 
   Local $idx
   Local $idx
;
 
   For $idx = 0 To UBound($a_nums01) - 1
   For $idx = 0 To UBound($a_nums01) - 1
       If StringIsFloat($a_nums01[$idx]) Or StringIsInt($a_nums01[$idx]) Then
       If StringIsFloat($a_nums01[$idx]) Or StringIsInt($a_nums01[$idx]) Then
Line 626: Line 816:
       EndIf
       EndIf
   Next
   Next
   _ArraySort($a_nums01, $MaxFlag)
   _ArraySort($a_nums01, $MaxFlag)
   If @NumParams = 3 Then
   If @NumParams = 3 Then
       For $idx = 0 To UBound($a_nums02) - 1
       For $idx = 0 To UBound($a_nums02) - 1
Line 635: Line 827:
             Return (0)
             Return (0)
         EndIf
         EndIf
      Next
  Next
 
       _ArraySort($a_nums02, $MaxFlag)
       _ArraySort($a_nums02, $MaxFlag)
      If $MaxFlag Then
 
  If $MaxFlag Then
         If Number($a_nums01[0]) > Number($a_nums02[0]) Then
         If Number($a_nums01[0]) > Number($a_nums02[0]) Then
             Return $a_nums01[0]
             Return $a_nums01[0]
Line 651: Line 845:
       EndIf
       EndIf
   EndIf
   EndIf
   Return $a_nums01[0]
   Return $a_nums01[0]
EndFunc ;==>_MinMaxa
EndFunc ;==>_MinMaxa
</syntaxhighlight>
</syntaxhighlight>
[[#top | ReturnToContents]]

Latest revision as of 07:30, 3 March 2017


Please always credit an author in your script if you use their code. It is only polite.


_1Dto2DArray

Author: guinness








#include <Array.au3>

Global $aArray_1D[2] = ["Data", "Data"]
_ArrayDisplay($aArray_1D)
_1Dto2DArray($aArray_1D)
_ArrayDisplay($aArray_1D)

Global $aArray_1D[10] = ["Data", "Data", "Data", "Data", "Data", "Data", "Data", "Data", "Data", "Data"]
_ArrayDisplay($aArray_1D)
_1Dto2DArray($aArray_1D, 9)
_ArrayDisplay($aArray_1D)

Func _1Dto2DArray(ByRef $aArray, Const $iAdditionalColumns = 1)
    Local Const $iSize = UBound($aArray, 1)

    Local $aReturn[$iSize][$iAdditionalColumns + 1]

    For $A = 0 To $iSize - 1
        $aReturn[$A][0] = $aArray[$A]
    Next

    $aArray = $aReturn

    Return $aReturn
EndFunc   ;==>_1Dto2DArray

ReturnToContents

_ArrayAddEx

Author: guinness








#include <Array.au3>

Example_1()
Example_2()

Func Example_1()
    Local $aArray_1[1] = ["Data_0"], $iBegin

    $iBegin = TimerInit() ; Start the timer.
    For $i = 1 To 5000
        _ArrayAdd($aArray_1, "Data_" & $i)
    Next
    $iBegin = TimerDiff($iBegin) ; End the timer and find the difference from start to finish.
    ConsoleWrite("_ArrayAdd from Array.au3 >> " & $iBegin & @CRLF)
    _ArrayDisplay($aArray_1)
EndFunc   ;==>Example_1

Func Example_2()
    Local $aArray_2[1] = ["Data_0"], $iBegin, $iDimension, $iCount

    $iBegin = TimerInit() ; Start the timer.
    For $i = 1 To 5000
        _ArrayAddEx($aArray_2, "Data_" & $i, $iDimension, $iCount) ; $iDimension is the overall size of the array & $iCount is the last index number, because regarding the overall size +1 as the next item will give false results as we're increasing the size of the array not by 1 but multiplying 2.
    Next
    $iBegin = TimerDiff($iBegin) ; End the timer and find the difference from start to finish.
    ConsoleWrite("_ArrayAddEx by guinness >> " & $iBegin & @CRLF)
    ReDim $aArray_2[$iCount] ; Remove the empty rows.
    _ArrayDisplay($aArray_2)
EndFunc   ;==>Example_2

Func _ArrayAddEx(ByRef $aArray, $sData, ByRef $iDimension, ByRef $iCount) ; Taken from Array.au3 and modified by guinness to reduce the use of ReDim.
    If IsArray($aArray) = 0 Then
        Return SetError(1, 0, -1)
    EndIf

    If UBound($aArray, 0) <> 1 Then
        Return SetError(2, 0, -1)
    EndIf

    If $iCount = 0 Then
        $iCount = UBound($aArray, 1)
    EndIf

    $iCount += 1
    If ($iCount + 1) >= $iDimension Then
        $iDimension = (UBound($aArray, 1) + 1) * 2
        ReDim $aArray[$iDimension]
    EndIf
    $aArray[$iCount - 1] = $sData
    Return $iCount - 1
EndFunc   ;==>_ArrayAddEx

ReturnToContents

_ArrayEmpty

Author: guinness







Creates Empty 1D & 2D Array's


#include <Array.au3>

Global $aMain[11] = [10]
_ArrayEmpty($aMain)
_ArrayDisplay($aMain, "Empty 1D Array.")

Global $aMainEx[11][3] = [[10]]
_ArrayEmpty($aMainEx)
_ArrayDisplay($aMainEx, "Empty 2D Array.")

Func _ArrayEmpty(ByRef $aArray)
    Switch UBound($aArray, 0) ; Dimension, 1D or 2D array are supported.
        Case 1
            Local $aEmpty1D[1]
            $aArray = $aEmpty1D

        Case 2
            ; Edit by DatMCEyeBall - Instead of emptying the array to [1][$x] we empty it to [$x][$y]
            ; where $x is the 1st dimensions size and $y the 2nd.
            ; Previous code was: Local $aEmpty2D[1][UBound($aArray, 2)]
            ; This way we preserve the arrays' original size.
            Local $aEmpty2D[UBound($aArray, 1)][UBound($aArray, 2)]
            $aArray = $aEmpty2D
    EndSwitch
EndFunc   ;==>_ArrayEmpty

ReturnToContents

_ArrayGet

Author: ProgAndy







Easy access for nested arrays up to 20 nested dimensions (including multidimensional arrays)


Global $b[2] = ["a", "b"]
Global $a[3][2] = [[0, -1], [$b, "x"], [1, -2]]

Global $c[5] = [11, 22, 33, 44, 55]

MsgBox(0, "", _ArrayGet($a, 1, 0, 1) & " # " & @error)

; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayGet
; Description ...: Gets a value from a nested multidimensional array
; Syntax ........: _ArrayGet(Byref $aArray, $i1[, $i2 = 0[, $i3 = 0[, $i4 = 0[, $i5 = 0[, $i6 = 0[, $i7 = 0[, $i8 = 0[,
;                 $i9 = 0[, $i10 = 0[, $i11 = 0[, $i12 = 0[, $i13 = 0[, $i14 = 0[, $i15 = 0[, $i16 = 0[, $i17 = 0[,
;                 $i18 = 0[, $i19 = 0[, $i20 = 0]]]]]]]]]]]]]]]]]]])
; Parameters ....: $aArray            - [in/out] An array of unknowns.
;                 $i1                 - first array index.
;                 $i2                 - [optional] second array index.
;                 $i3                 - [optional] third array index.
;                 $i4                 - [optional] fourth array index.
;                 $i5                 - [optional] fifth array index.
;                 $i6                 - [optional] sixth array index.
;                 $i7                 - [optional] seventh array index.
;                 $i8                 - [optional] eighth array index.
;                 $i9                 - [optional] ninth array index.
;                 $i10               - [optional] 10th array index.
;                 $i11               - [optional] 11th array index.
;                 $i12               - [optional] 12th array index.
;                 $i13               - [optional] 13th array index.
;                 $i14               - [optional] 14th array index.
;                 $i15               - [optional] 15th array index.
;                 $i16               - [optional] 16th array index.
;                 $i17               - [optional] 17th array index.
;                 $i18               - [optional] 18th array index.
;                 $i19               - [optional] 19th array index.
;                 $i20               - [optional] 20th array index.
; Return values .: Success: Value at the specified index
;                 Error: Nothing, Sets @error to:
;                 |1 - Trying to acces non-array as array
;                 |2 - Not enough dimensions for the last accessed array
;                 |3 - Index out of bounds for a dimension
;                 @extended contains the dimension in error (according to parameter list)
; Author ........: ProgAndy
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _ArrayGet(ByRef $aArray, $i1, $i2=0, $i3=0, $i4=0, $i5=0, $i6=0, $i7=0, $i8=0, $i9=0, $i10=0, $i11=0, $i12=0, $i13=0, $i14=0, $i15=0, $i16=0, $i17=0, $i18=0, $i19=0, $i20=0)
	#forceref $i1, $i2, $i3, $i4, $i5, $i6, $i7, $i8, $i9, $i10, $i11, $i12, $i13, $i14, $i15, $i16, $i17, $i18, $i19, $i20
   ; ProgAndy
    Local $NULL
    If Not IsArray($aArray) Then Return SetError(1, 1, $NULL)
    Local $iDims = UBound($aArray, 0)
    If @NumParams <= $iDims Then Return SetError(2, @NumParams, $NULL)
    Local $sAccess = "$aArray"
    For $i = 1 To $iDims
        Local $iIndex = Int(Eval("i" & $i))
        If $iIndex >= UBound($aArray, $i) Then Return SetError(3, $i, $NULL)
        $sAccess &= '[' & $iIndex & ']'
    Next
    If @NumParams-1 > $iDims Then ; Still indices left
        For $i = $iDims+1 To @NumParams-1
            $sAccess &= ", $i" & $i
        Next
        Local $vResult = Execute("_ArrayGet(" & $sAccess & ")")
        If @error Then SetError(@error, @extended+$iDims)
        Return $vResult
    Else ;
        Return Execute($sAccess)
    EndIf
EndFunc

ReturnToContents

_ArraySet

Author: ProgAndy







Easy access for nested arrays up to 20 nested dimensions (including multidimensional arrays)


Global $b[2] = ['a', 'b'
Global $a[3][2] = [[0, -1], [$b, "x"], [1, -2]]

Global $c[5] = [11, 22, 33, 44, 55]

_ArraySet($a, $c, 1, 0, 0)

; #FUNCTION# ====================================================================================================================
; Name ..........: _ArraySet
; Description ...: Sets a value from a nested multidimensional array
; Syntax ........: _ArraySet(Byref $aArray, $vValue, $i1[, $i2 = 0[, $i3 = 0[, $i4 = 0[, $i5 = 0[, $i6 = 0[, $i7 = 0[, $i8 = 0[,
;                 $i9 = 0[, $i10 = 0[, $i11 = 0[, $i12 = 0[, $i13 = 0[, $i14 = 0[, $i15 = 0[, $i16 = 0[, $i17 = 0[,
;                 $i18 = 0[, $i19 = 0[, $i20 = 0]]]]]]]]]]]]]]]]]]])
; Parameters ....: $aArray            - [in/out] An array of unknowns.
;                 $vValue             - value to set at specified index.
;                 $i1                 - first array index.
;                 $i2                 - [optional] second array index.
;                 $i3                 - [optional] third array index.
;                 $i4                 - [optional] fourth array index.
;                 $i5                 - [optional] fifth array index.
;                 $i6                 - [optional] sixth array index.
;                 $i7                 - [optional] seventh array index.
;                 $i8                 - [optional] eighth array index.
;                 $i9                 - [optional] ninth array index.
;                 $i10               - [optional] 10th array index.
;                 $i11               - [optional] 11th array index.
;                 $i12               - [optional] 12th array index.
;                 $i13               - [optional] 13th array index.
;                 $i14               - [optional] 14th array index.
;                 $i15               - [optional] 15th array index.
;                 $i16               - [optional] 16th array index.
;                 $i17               - [optional] 17th array index.
;                 $i18               - [optional] 18th array index.
;                 $i19               - [optional] 19th array index.
;                 $i20               - [optional] 20th array index.
; Return values .: Success: True
;                 Error: False, Sets @error to:
;                 |1 - Trying to acces non-array as array
;                 |2 - Not enough dimensions for the last accessed array
;                 |3 - Index out of bounds for a dimension
;                 |all: - @extended contains the dimension in error (according to parameter list)
; Author ........: ProgAndy
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _ArraySet(ByRef $aArray, Const $vValue, $i1, $i2=0, $i3=0, $i4=0, $i5=0, $i6=0, $i7=0, $i8=0, $i9=0, $i10=0, $i11=0, $i12=0, $i13=0, $i14=0, $i15=0, $i16=0, $i17=0, $i18=0, $i19=0, $i20=0)
    ; ProgAndy
    Local Const $NULL = False

    If Not IsArray($aArray) Then Return SetError(1, 1, $NULL)

    Local $iDims = UBound($aArray, 0)

    If @NumParams-2 < $iDims Then Return SetError(2, @NumParams-1, $NULL)

    Local $sAccess = "$aArray"

    For $i = 1 To $iDims
        Local $iIndex = Int(Eval("i" & $i))
        If $iIndex >= UBound($aArray, $i) Then Return SetError(3, $i, $NULL)
        $sAccess &= '[' & $iIndex & ']'
    Next

    If @NumParams-2 > $iDims Then
        $sAccess &= ", $vValue"
        For $i = $iDims+1 To @NumParams-2
            $sAccess &= ", $i" & $i
        Next
        Local $fSuccess = Execute("_ArraySet(" & $sAccess & ")")
        If @error Then SetError(@error, @extended+$iDims)
        Return $fSuccess
    EndIf

    Switch $iDims
        Case 1
            $aArray[$i1] = $vValue
        Case 2
            $aArray[$i1][$i2] = $vValue
        Case 3
            $aArray[$i1][$i2][$i3] = $vValue
        Case 4
            $aArray[$i1][$i2][$i3][$i4] = $vValue
        Case 5
            $aArray[$i1][$i2][$i3][$i4][$i5] = $vValue
        Case 6
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6] = $vValue
        Case 7
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7] = $vValue
        Case 8
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8] = $vValue
        Case 9
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9] = $vValue
        Case 10
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10] = $vValue
        Case 11
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11] = $vValue
        Case 12
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12] = $vValue
        Case 13
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12][$i13] = $vValue
        Case 14
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12][$i13][$i14] = $vValue
        Case 15
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12][$i13][$i14][$i15] = $vValue
        Case 16
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12][$i13][$i14][$i15][$i16] = $vValue
        Case 17
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12][$i13][$i14][$i15][$i16][$i17] = $vValue
        Case 18
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12][$i13][$i14][$i15][$i16][$i17][$i18] = $vValue
        Case 19
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12][$i13][$i14][$i15][$i16][$i17][$i18][$i19] = $vValue
        Case 20
            $aArray[$i1][$i2][$i3][$i4][$i5][$i6][$i7][$i8][$i9][$i10][$i11][$i12][$i13][$i14][$i15][$i16][$i17][$i18][$i19][$i20] = $vValue
	EndSwitch

    Return True
EndFunc

ReturnToContents

_ArrayLength

Author: guinness








Global $aArray[2][50]
ConsoleWrite(_ArrayLength($aArray, 2) & @CRLF) ;= > 10 - Total items
ConsoleWrite(_ArrayLength($aArray, 1) & @CRLF) ;= > 5 - Columns
ConsoleWrite(_ArrayLength($aArray, 0) & @CRLF) ;= > 2 - Rows

ConsoleWrite(@CRLF)

Global $aArray[2]
ConsoleWrite(_ArrayLength($aArray, 2) & @CRLF) ;= > 2 - Total items
ConsoleWrite(_ArrayLength($aArray, 1) & @CRLF) ;= > 1 - Columns
ConsoleWrite(_ArrayLength($aArray, 0) & @CRLF) ;= > 2 - Rows

Func _ArrayLength($aArray, $iType = 1)
    Local $iDim = UBound($aArray, 0)

    If $iDim < 0 Or $iDim > 2 Then ; If not a 1D or 2D Array, then return -1 and set @error to non-zero.
        Return SetError(1, 0, -1)
    EndIf

    Switch $iType
        Case 2
            If $iDim = 2 Then
                Return UBound($aArray, 2) * UBound($aArray, 1)
            Else
                Return UBound($aArray, 1)
            EndIf

        Case 1
            If $iDim = 2 Then
                Return UBound($aArray, 2)
            Else
                Return 1
            EndIf

        Case Else
            Return UBound($aArray, 1)
    EndSwitch
EndFunc   ;==>_ArrayLength

ReturnToContents

_ArraySortNum

Author: SmOke_N








; ArraySortNum(ByRef $nArray, $Ascending = 0, $Start = 1)
; Sorts arrays based on TRUE Numerical Order, including Decimals

#include <Array.au3>

; Example sort Array created with StringSplit.
Global $avArray = StringSplit("6,8,10,4,2,13,12,129,106.1,106.4", ",")

_ArraySortNum($avArray, 1)
_ArrayDisplay($avArray, "Sorted Desc starting at 1")

Func _ArraySortNum(ByRef $nArray, $Ascending = 0, $Start = 1)
	For $i = $Start To UBound($nArray) - 2
		Local $SE = $i

		If $Ascending = 0 Then
			For $x = $i To UBound($nArray) - 1
				If Number($nArray[$SE]) < Number($nArray[$x]) Then $SE = $x
			Next
		Else
			For $x = $i To UBound($nArray) - 1
				If Number($nArray[$SE]) > Number($nArray[$x]) Then $SE = $x
			Next
		EndIf

		Local $HLD = $nArray[$i]

		$nArray[$i] = $nArray[$SE]

		$nArray[$SE] = $HLD
	Next
EndFunc   ;==>_ArraySortNum

ReturnToContents

_AssocArray

Author: MilesAhead








; use Scripting.Dictionary object for simple associative arrays
; with string keys. Key comparisons are case insensitive.

Global $myArray
$myArray = _AssocArray()
If @error Then
    MsgBox(0x1010,"_AssocArray() Error", "Error Creating Associative Array!")
    Exit
EndIf

$myArray("AntiqueWhite") = 0xFAEBD7
$myArray("Black") = 0x000000
$myArray("Blue") = 0x0000FF
$myArray("Brown") = 0xA52A2A
$myArray("CadetBlue") = 0x5F9EA0
$myArray("Chocolate") = 0xD2691E
$myArray("Coral") = 0xFF7F50

MsgBox(0x1040,"","Hex for Chocolate Color is: 0x" & Hex($myArray("Chocolate"),6))
_AssocArrayDestroy($myArray)

Func _AssocArray()
    Local $aArray = ObjCreate("Scripting.Dictionary")

    If @error Then
        Return SetError(1, 0, 0)
    EndIf

    $aArray.CompareMode = 1

    Return $aArray
EndFunc   ;==>_AssocArray

Func _AssocArrayDestroy(ByRef $aArray)
    If Not IsObj($aArray) Then
        Return False
    EndIf
    $aArray.RemoveAll()
    $aArray = 0
    Return True
EndFunc   ;==>_AssocArrayDestroy

ReturnToContents

_DynEnumArray

Author: spudw2k








Func _DynEnumArray($aArrayToEnum)
    Local $aDims[1][2]=[["ElementIdx","ElementCount"]]   ;Create an Array to Track Dimensions and Elements
    For $iX = 1 to UBound($aArrayToEnum,0)
        ReDim $aDims[$iX+1][2]
        $aDims[$iX][0]=0
        $aDims[$iX][1]=UBound($aArrayToEnum,$iX)
    Next
    Do   ;Loop Through Array Elements
        $sArray = "$aArrayToEnum"
        For $iX = 1 to UBound($aDims)-1
            $sArray &= "[" & $aDims[$iX][0] & "]"
        Next
        ConsoleWrite($sArray & " " & Execute($sArray) & @CRLF)  ;Output Subscript and Value

        $aDims[UBound($aDims)-1][0] += 1   ;Increment Last Dimension Element
        For $iY = UBound($aDims)-2 To 1 Step -1   ;Increment Dimension Element
            If $aDims[$iY+1][0] = $aDims[$iY+1][1] Then
                $aDims[$iY+1][0]=0
                $aDims[$iY][0]+=1
            EndIf
        Next

    Until $aDims[1][0]=$aDims[1][1]
    Return ConsoleWrite(@CRLF)
EndFunc

;Demos
Local $aArray[5]=["Never","Gonna","Give","You","Up"]
_DynEnumArray($aArray)

Local $aArray[6][2]=[["Penn", "Teller"],["Gilbert", "Sullivan"],["Sonny", "Cher"],["Stone", "Parker"],["Fischbacher", "Horn"],["Plant", "Page"]]
_DynEnumArray($aArray)

Local $aArray[2][3][2]=[[["All","Your"],["Base","Are"],["Belong","To Us"]],[["Is","This"],["The","Way"],["Back","Home"]]]
_DynEnumArray($aArray)

ReturnToContents

_FuncListToArray

Author: GEOSoft








$aFlist = _FuncListToArray(@ScriptFullPath)
If NOT @Error Then
   For $i = 0 To Ubound($aFlist) -1
      MsgBox(0, "TEST", $aFlist[$i], 2)
   Next
EndIf

Func _FuncListToArray($sStr)
   If FileExists($sStr) Then $sStr = FileRead($sStr)
   Local $sRegEx = "(?i)(?m:^|\n)\s*Func\s+(\w+)\s*\("
   $aRegEx = StringRegExp($sStr, $sRegEx, 3)
   If IsArray($aRegEx) Then Return $aRegEx
   Return SetError(1)
EndFunc

ReturnToContents

Identify Duplicate Values In Arrays

Author: GaryFrost








; Identify Duplicate Values In Arrays
#include <Array.au3>

Global $a1[3] = [1, 2, 3], $a2[5] = [5, 6, 7, 8, 9], $a3[2] = [3, 5]
Global $a_a[4] = [3, $a1, $a2, $a3]
MsgBox(0, "Dupes?", _SearchForDupes($a_a))

Func _SearchForDupes($a_arrays, $i_ReturnType = 0)
	Local $Dupes[1] = [0]

	If $a_arrays[0] < 2 Then Return ''

	For $i = 1 To $a_a[0] - 1
		For $j = $i + 1 To $a_a[0]
			_FindDupe($a_a[$i], $a_a[$j], $Dupes)
		Next
	Next

	If $Dupes[0] = 0 Then Return ''

	_ArraySort($Dupes)

	If Not $i_ReturnType Then
		Local $s_return = ''

		For $x = 1 To $Dupes[0]
			$s_return &= $Dupes[$x] & ','
		Next
		Return StringTrimRight($s_return, 1)
	Else
		Return $Dupes
	EndIf
EndFunc   ;==>_SearchForDupes

Func _FindDupe(ByRef $a_array01, ByRef $a_array02, ByRef $Dupes)
	Local $found = False

	For $i = 0 To UBound($a_array01) - 1
		For $j = 0 To UBound($a_array02) - 1
			If $a_array01[$i] = $a_array02[$j] Then
				$found = False

				For $x = 1 To $Dupes[0]
					If $Dupes[$x] = $a_array01[$i] Then
						$found = True
						ExitLoop
					EndIf
				Next

				If Not $found Then
					$Dupes[0] += 1
					ReDim $Dupes[$Dupes[0] + 1]
					$Dupes[$Dupes[0]] = $a_array01[$i]
				EndIf
			EndIf
		Next
	Next
EndFunc   ;==>_FindDupe

ReturnToContents

_Is1DArray

Author: guinness








Local $sString = 'String'
Local $a1DArray[2] = [1, 'Row_1']
Local $a2DArray[2][2] = [[1],['Row_1: Col_1', 'Row_1: Col_2']]

ConsoleWrite('Is the variable $sString a 1D Array?: ' & _Is1DArray($sString) & @CRLF)
ConsoleWrite('Is the variable $a1DArray a 1D Array?: ' & _Is1DArray($a1DArray) & @CRLF)
ConsoleWrite('Is the variable $a2DArray a 1D Array?: ' & _Is1DArray($a2DArray) & @CRLF)

; Check if a variable is a 1D Array.
Func _Is1DArray(ByRef Const $aArray)
	Return UBound($aArray) And (UBound($aArray, 0) = 1)
EndFunc   ;==>_Is1DArray

ReturnToContents

_Is2DArray

Author: guinness







Check if a variable is a 2D Array.


Local $sString = 'String'
Local $a1DArray[2] = [1, 'Row_1']
Local $a2DArray[2][2] = [[1],['Row_1: Col_1', 'Row_1: Col_2']]

ConsoleWrite('Is the variable $sString a 1D Array?: ' & _Is2DArray($sString) & @CRLF)
ConsoleWrite('Is the variable $a1DArray a 1D Array?: ' & _Is2DArray($a1DArray) & @CRLF)
ConsoleWrite('Is the variable $a2DArray a 1D Array?: ' & _Is2DArray($a2DArray) & @CRLF)

; Check if a variable is a 2D Array.
Func _Is2DArray(ByRef Const $aArray)
	Return UBound($aArray) And (UBound($aArray, 0) = 2)
EndFunc   ;==>_Is2DArray

ReturnToContents

_IsInBounds

Author: guinness








#include <Constants.au3>

Local $aArray[5] = ['Item_1', 'Item_2', 'Item_3', 'Item_4', 'Item_5']

ConsoleWrite(_IsInBounds($aArray, 1) & @CRLF) ; Returns True as the Array has 5 items.
ConsoleWrite(_IsInBounds($aArray, 5) & @CRLF) ; Returns False as the Array has 5 items but starts from the 0th index not 1st.
ConsoleWrite(_IsInBounds($aArray, 0) & @CRLF) ; Returns True.

If _IsInBounds($aArray, 4) Then
	MsgBox($MB_SYSTEMMODAL, '', 'Position 4 contains the value: "' & $aArray[4] & '"')
EndIf

Func _IsInBounds(ByRef Const $aArray, $iIndex)
	Local $iDim = UBound($aArray, 0)
	If $iDim < 0 Or $iDim > 2 Then ; If not a 1D or 2D Array, then return -1 and set @error to non-zero.
		Return SetError(1, 0, False)
	EndIf
	Return $iIndex < UBound($aArray, 1)
EndFunc   ;==>_IsInBounds

ReturnToContents

_MultiFileListToArray

Author: Valuater








; Multiple File List to Array

$Files_List = _MultiFileListToArray(@ScriptDir, "*.txt|*.ini")

; for display only
$GUI = GUICreate(" _MultiFileListToArray - DEMO")
$Edit1 = GUICtrlCreateEdit("", 50, 50, 300, 300)
For $x = 1 To UBound($Files_List) - 1
    GUICtrlSetData(-1, $Files_List[$x] & @CRLF, 1)
Next
GUISetState()

While GUIGetMsg() <> -3
WEnd
; end display only

Func _MultiFileListToArray($sPath, $sFilter = "*", $iFlag = 0)
    Local $hSearch, $sFile, $asFileList[1], $sCount
    If Not FileExists($sPath) Then Return SetError(1, 1, "")
    If (StringInStr($sFilter, "\")) Or (StringInStr($sFilter, "/")) Or (StringInStr($sFilter, ":")) Or (StringInStr($sFilter, ">")) Or (StringInStr($sFilter, "<")) Or (StringStripWS($sFilter, 8) = "") Then Return SetError(2, 2, "")
    $sFilter = (StringSplit($sFilter, "|"))
    If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
    For $sCount = 1 To $sFilter[0]
        $hSearch = FileFindFirstFile($sPath & "\" & $sFilter[$sCount])
        If $hSearch = -1 Then
            If $sCount = $sFilter[0] Then Return SetError(4, 4, $asFileList)
            ContinueLoop
        EndIf
        While 1
            $sFile = FileFindNextFile($hSearch)
            If @error Then
                SetError(0)
                ExitLoop
            EndIf
            If $iFlag = 1 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") <> 0 Then ContinueLoop
            If $iFlag = 2 And StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D") = 0 Then ContinueLoop
            ReDim $asFileList[UBound($asFileList) + 1]
            $asFileList[0] = $asFileList[0] + 1
            $asFileList[UBound($asFileList) - 1] = $sFile
        WEnd
        FileClose($hSearch)
    Next
    Return $asFileList
EndFunc   ;==>_MultiFileListToArray

ReturnToContents

Return Min or Max Number From One or Two Arrays

Author: GaryFrost








#include <Array.au3>

Func _MinMaxa($a_nums01, $MaxFlag = 0, $a_nums02 = 0)
   If Not IsArray($a_nums01) Then
      SetError(1)
      Return (0)
   EndIf

   If @NumParams = 3 Then
      If Not IsArray($a_nums02) Then
         SetError(3)
         Return (0)
      EndIf
   EndIf

   Local $idx

   For $idx = 0 To UBound($a_nums01) - 1
      If StringIsFloat($a_nums01[$idx]) Or StringIsInt($a_nums01[$idx]) Then
         $a_nums01[$idx] = Number($a_nums01[$idx])
      Else
         SetError(2)
         Return (0)
      EndIf
   Next

   _ArraySort($a_nums01, $MaxFlag)

   If @NumParams = 3 Then
      For $idx = 0 To UBound($a_nums02) - 1
         If StringIsFloat($a_nums02[$idx]) Or StringIsInt($a_nums02[$idx]) Then
            $a_nums02[$idx] = Number($a_nums02[$idx])
         Else
            SetError(4)
            Return (0)
         EndIf
	  Next

      _ArraySort($a_nums02, $MaxFlag)

	  If $MaxFlag Then
         If Number($a_nums01[0]) > Number($a_nums02[0]) Then
            Return $a_nums01[0]
         Else
            Return $a_nums02[0]
         EndIf
      Else
         If Number($a_nums01[0]) < Number($a_nums02[0]) Then
            Return $a_nums01[0]
         Else
            Return $a_nums02[0]
         EndIf
      EndIf
   EndIf

   Return $a_nums01[0]
EndFunc ;==>_MinMaxa

ReturnToContents