Sign in to follow this  
Followers 0
Khab

Matching a substring to any element in an array

4 posts in this topic

Hey all, I'm hoping someone can assist with an efficient way to do this. I tried searching, but didn't really find exactly what I'm after (though it is possible I just used bad search terms).

Basically, I have a script that reads through each line in a text file using FileReadLine. As it reads each line, I need to check the line to see if any words, which are stored in an array (although this can be stored differently if there is a better way), are in the line. I have an event to trigger, if any of the array words is found in the line.

E.g. I might have a word list:

John

Steve

Mary

Lisa

And a line like:

Last week John went to the shops with Mary

In this case, the event should trigger, as one (or more, it doesn't matter) of the array list strings was found in the line.

At the moment, I'm basically stepping through the array with a For loop, generating a regex containing the array element, comparing it to the current line being read, and if it matches, then trigger the event.

However, this seems to be very slow & inefficient - is there a better way to do this?

Cheers!

Share this post


Link to post
Share on other sites



I'm basically stepping through the array with a For loop, generating a regex containing the array element,

Which array? The search array? Why generate it everytime? Perhaps using stringinstr would be faster? Would it be faster to read the entire file to memory rather than doing one line at a time?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Which array? The search array? Why generate it everytime? Perhaps using stringinstr would be faster? Would it be faster to read the entire file to memory rather than doing one line at a time?

Hi,

have a look at _FileReadToArray and _ArraySearch (could be a solution as well):

#Include <File.au3>
#Include <Array.au3>
$sFilepath = "MyTextFile.txt"
_FileReadToArray($sFilePath, $aArray)
For $i = 1 to UBound ($bArray_for_comparing) - 1 ; MAYBE $i=0 depends on the structure of $bArray....
    If _ArraySearch ($aArray, $bArray_for_comparing [$i]) > 0 Then
        TriggerEvent...
        ExitLoop ; Maybe
    endif
Next

You can test speed by adding _Timer_Init and _Timer_Diff at your code (see helpfile). So you can test, which is the fastest solution.

;-))

Stefan

Edited by 99ojo

Share this post


Link to post
Share on other sites

Try it:

#Include <File.au3>

Dim $aArray
Local $sLookingFor = 'John|Steve|Mary|Lisa'
Local $sSearch = '(?i)^' & $sLookingFor 

$sFilepath = "MyTextFile.txt"
_FileReadToArray($sFilePath, $aArray)

For $i = 1 To $aArray[0]
    If StringRegExp($aArray[$i], $sSearch) Then
        ; TriggerEvent...
        ConsoleWrite($aArray[$i] & @CRLF)      
    endif
Next


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

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  
Followers 0