Jump to content

_ArrayPatternMatch


funkey
 Share

Recommended Posts

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...