Jump to content

General help - Code improvement - multiple searches in a file


Recommended Posts

Hi everybody,

i want to optimize my search code, because i don't think that my solution is "acceptable" from development perspective, it might be optimized.

I have a text file, i read it and i copy all the entries to an Array, then i have to start a search to see if a specific entry is present. I have about ten different strings to search.

Func FileSearch($file_content_array, $search)
    $j=0
    For $i = 0 To UBound($file_content_array) - 1
            $search_result=StringInStr($file_content_array[$i],$search)
                If $search_result<>0 Then
                    ReDim $searchResultArray[UBound($searchResultArray) + 1]
                    $searchResultArray [$j] = $file_content_array[$i]
                $j+=1
                EndIf

    Next
    Return $searchResultArray
EndFunc

My solution would be ok for a single search, but in case of multiple searches? would it be still good?

I have thought to use a 2D Array, where the first column is the array-item and the second column is "yes" or "no". But what about the searches? Do i have to use 10 times the function?

In case of questions: I have to scan a directory and check whether some files are present or not and then write into another file:"yes, it is present", "no, it is not present".

Thanks in advance

Edited by LoneWolf_2106
Link to comment
Share on other sites

Here is a way using a regex to search several string at the same time :

#include <Array.au3>

Local $sInputFile = @ScriptFullPath ; your file
Local $aSearch = ["FileRead", "StringRegExp"] ; string to search for

Local $sContent = FileRead($sInputFile)
Local $sSearchPatten = "(?i)(?:^|\R)\V*(?:"
For $i = 0 To UBound($aSearch) - 1
    $sSearchPatten &= ($i ? "|" : "") & "\Q" & $aSearch[$i] & "\E"
Next
$sSearchPatten &= ")\V*"
ConsoleWrite($sSearchPatten)
Local $aResults = StringRegExp($sContent, $sSearchPatten , 3)

_ArrayDisplay($aResults)

 

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

×
×
  • Create New...