Jump to content
Sign in to follow this  
weaponx

Recursive Multidimensional Array Search

Recommended Posts

weaponx

MultidimensionalSearch.au3

NOTE: Attachment doesn't contain examples below.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.8.1
 Author:         WeaponX

 Script Function:
    Recursively search multidimensional array (any number of dimensions)
    
 Usage:
    _SearchMultiDimensional(ByRef $smdArray, $smdValue, $smdCaseSense = False, $smdPartialMatch = False, $smdDepth = 1, $smdElement = "$smdArray")
    smdArray = Multidimensional Array
    smdValue = The value to search for
    smdCaseSense = Case sensitivity
    smdPartialMatch = Alloe partial matches
    smdDepth = Current dimension, starting at 1
    smdElement = String to execute

#ce ----------------------------------------------------------------------------

Dim $test[4] = ["Red", "Green", "Blue", "Orange"]

;Returns 3
MsgBox(0,"",_SearchMultiDimensional($test,"ang", false, true))

;3 dimensional array
Dim $array[4][4][4]

$array[0][0][0] = "Ford"
    $array[0][1][0] = "Mustang"
        $array[0][1][1] = "Red"
        $array[0][1][2] = "Manual"
        $array[0][1][3] = "1996"
    $array[0][2][0] = "F-150"
        $array[0][2][1] = "Red"
        $array[0][2][2] = "Manual"
        $array[0][2][3] = "1996"    
    $array[0][3][0] = "Taurus"
        $array[0][3][1] = "Red"
        $array[0][3][2] = "Manual"
        $array[0][3][3] = "1996"

$array[1][0][0] = "Nissan"
    $array[1][1][0] = "300ZX"
        $array[1][1][1] = "Red"
        $array[1][1][2] = "Manual"
        $array[1][1][3] = "1996"
    $array[1][2][0] = "350Z"
        $array[1][2][1] = "Red"
        $array[1][2][2] = "Manual"
        $array[1][2][3] = "1996"
    $array[1][3][0] = "Skyline"
        $array[1][3][1] = "Red"
        $array[1][3][2] = "Manual"
        $array[1][3][3] = "1996"

$array[2][0][0] = "Honda"
    $array[2][1][0] = "Civic"
        $array[2][1][1] = "Blue"
        $array[2][1][2] = "Manual"
        $array[2][1][3] = "1996"
    $array[2][2][0] = "S2000"
        $array[2][2][1] = "Red"
        $array[2][2][2] = "Manual"
        $array[2][2][3] = "1996"
    $array[2][3][0] = "Pilot"
        $array[2][3][1] = "Red"
        $array[2][3][2] = "Manual"
        $array[2][3][3] = "1996"

;Returns 1,1,0
MsgBox(0,"",_SearchMultiDimensional($array,"ZX", false, true))

;Returns 2,1,1
MsgBox(0,"",_SearchMultiDimensional($array,"BLUE", false))

;Returns 2,1,1
MsgBox(0,"",_SearchMultiDimensional($array,"Blue"))

;Returns NULL
MsgBox(0,"",_SearchMultiDimensional($array,"BLUE", true))

;smdArray = Multidimensional Array
;smdValue = The value to search for
;smdCaseSense = Case sensitivity
;smdPartialMatch = Alloe partial matches
;smdDepth = Current dimension, starting at 1
;smdElement = String to execute

Func _SearchMultiDimensional(ByRef $smdArray, $smdValue, $smdCaseSense = False, $smdPartialMatch = False, $smdDepth = 1, $smdElement = "$smdArray")

    ;Loop through all elements within the current dimension
    For $X = 0 to Ubound($smdArray, $smdDepth)
        
        ;Recurse only to # of dimensions
        If $smdDepth < Ubound($smdArray, 0) Then
            $smdResult = _SearchMultiDimensional($smdArray, $smdValue, $smdCaseSense, $smdPartialMatch, $smdDepth + 1, $smdElement & "[" & $X & "]")
            If $smdResult <> "" Then Return $X & "," & $smdResult
        EndIf
        
        ;If case sensitive
        If $smdCaseSense Then
            ;If partial match is allowed
            If $smdPartialMatch Then
                If StringInStr(Execute($smdElement & "[" & $X & "]"),$smdValue,1) Then Return String($X)
            Else
                If Execute($smdElement & "[" & $X & "]") == $smdValue Then Return String($X)
            EndIf
        Else
            ;If partial match is allowed
            If $smdPartialMatch Then
                If StringInStr(Execute($smdElement & "[" & $X & "]"),$smdValue,0) Then Return String($X)
            Else
                If Execute($smdElement & "[" & $X & "]") = $smdValue Then Return String($X)
            EndIf
        EndIf
    Next
    
    Return ""
EndFunc

Edit: I found this first but I like the challenge of writing recursive functions and mine is only 30 lines vs. 70 lines of code

Edited by weaponx

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  

×