Vectorized String Functions in AutoIt

1. Introduction

In my current workplace at Leverage Retirement, Inc., I use Autoit to automate file management operations, such as copying files to archive directories and checking whether files exist for the morning processes. These operations involve the application of regular expressions to find out whether specific files match a given pattern (e.g. Deposit \d{4}-\d{2}-\d{2}, where \d{x} represents a number of x digits). While there are many string functions in AutoIt, most of them apply only to constants and not arrays. As such, to perform string functions on arrays, a loop has to be employed. However, because multiple loops may bloat the script, I’ve developed vectorized string functions so that I can apply string operations over arrays in a more concise manner.

Below are some of these functions. Give them a try!

2. StringSub()/StringSubV()

The functions StringSub() is simply a shorthand version of StringRegExpReplace(), since the latter is inefficient to type out every time you need to use it. StringSubV()is a vectorized version of StringSub(), performing over an array.

Here is how they are defined1 in AutoIt:

; StringSub()
Func StringSub($string, $search, $replace)

    Return StringRegExpReplace($string, $search, $replace)

EndFunc

; StringSubV()
Func StringSubV($a, $search, $replace)

  For $i = 0 to UBound($a) - 1
  
    $a[$i] = StringSub($a[$i], $search, $replace)
    
  Next

    Return $a

EndFunc

Here is an example of applying StringSubV().

#include <Array.au3> ; for displaying arrays

Local $gems[] = ["Ruby", "Crystal", "Emerald"]

$gems2 = StringSubV($gems, "r", "Z")

_ArrayDisplay($gems2)

3. StringDetect()/StringDetectV()

The function StringDetect() determines whether a constant string matches a regular expression pattern, returning a Boolean value. The function StringDetectV() applies StringDetect() to each element in an array. Ultimately, StringDetectV() is the equivalent of grepl() in R.

; StringDetect()
Func StringDetect($string, $pattern)

    If StringRegExp($string, $pattern) = 1 Then

        $x = True

    Else

        $x = False

    EndIf

    Return $x

EndFunc

; StringDetectV()
Func StringDetectV($a, $pattern) ; $a is an array

    For $i = 0 to UBound($a) - 1

        $a[$i] = StringDetect($a[$i], $pattern)

    Next

    Return $a

EndFunc

Here is an example of StringDetectV():

Local $gems[] = ["Ruby", "Crystal", "Emerald"]

$gems2 = StringDetectV($gems, "r")

_ArrayDisplay($gems2)

4. StringPos() & StringSubset()

The function StringPos() returns the indices of a regular expression pattern match, while StringSubset() extracts only the elements in an array matching a pattern.

; StringPos()
Func StringPos($a, $search_pattern)

  ; 3 = regular expression pattern
    $indices = _ArrayFindAll($a, $search_pattern, Default, Default, Default, 3)

    Return $indices

EndFunc

; StringSubset()
Func StringSubset($a, $search_pattern)

    $indices = StringPos($a, $search_pattern) ; find the indices

    Local $output[UBound($indices)] = [0] ; initialize loop

    ; Insert into $indices the matching values from $a.
    For $i = 0 to UBound($indices) - 1

        $output[$i] = $a[$indices[$i]]

    Next

    ; Return the array.
    Return $output

EndFunc

Here is an example of using StringSubset(), which uses StringPos():

#include <Array.au3> ; for displaying arrays

Local $gems[] = ["Ruby", "Crystal", "Emerald"]

$gems2 = StringSubset($gems, "r")

_ArrayDisplay($gems2)

5. Conclusion

The functions provided in this post vectorize existing string operations in AutoIt. StringSubV() substitutes each string in an array with a defined replacement string, being a vectorized version of StringRegExpReplace(). StringDetectV() determines whether each string in an array matches a given pattern, being a vectorized version of StringRegExp(). Finally, StringSubset() extracts only the strings in an array that match a stated pattern, being a simpler version of _ArrayFindAll(). In summary, I hope that these functions will help you in your work when managing arrays of strings.

You can find these functions and more in my RS_AutoIt GitHub repository.


  1. StringSubV() is defined slightly differently in this post than in my GitHub repository so that you may run these functions independently of the other functions in my GitHub repository (i.e. you may run these functions in this post without dependencies).↩︎