Jump to content
Sign in to follow this  
funkey

_ArrayPatternMatch

Recommended Posts

funkey

Use wildcards (* and ?) for filtering your arrays.

#Include <Array.au3>

Local $aTest
Local $aArray[5] = ["abcde", "aabcd", "bcd", "aeiou", "asdf"]
Local $aPattern[6] = ["*e", "aa*", "?cd", "*e*", "*d", "*d?"]

_ArrayDisplay($aArray, "Original Array")

For $i = 0 To UBound($aPattern) - 1
    $aTest = _ArrayPatternMatch($aArray, $aPattern[$i])
    _ArrayDisplay($aTest, "Filter: " & $aPattern[$i])
Next

Func _ArrayPatternMatch($aArray, $sPattern)
    ;funkey May 17, 2010
    If UBound($aArray, 0) <> 1 Then Return SetError(1, 0, "")   ;no 1D-array
    Local $n = UBound($aArray)
    Local $aRes[$n], $j = 0
    $sPattern = "^\Q" & $sPattern & "\E$"
    $sPattern = StringReplace($sPattern, "?", "\E.\Q")
    $sPattern = StringReplace($sPattern, "*", "\E.*\Q")
    For $i = 0 To $n - 1
        If StringRegExp($aArray[$i], $sPattern, 0) Then
            $aRes[$j] = $aArray[$i]
            $j += 1
        EndIf
    Next
    If $j = 0 Then Return SetError(2, 0, "")    ;no match
    ReDim $aRes[$j]
    Return $aRes
EndFunc

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
dmob

Nice work, think I might find a use for this.

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  

×