LoneWolf_2106 Posted June 22, 2017 Share Posted June 22, 2017 Hi everybody, i have created a simple search function for a text file and i have stored the result in an array of strings. Func FileSearch1() For $i = 0 To UBound($content_array) - 1 $search_result=StringInStr($content_array[$i],$search) If $search_result<>0 Then $searchResultArray[$i] = $content_array[$i] Next EndFunc I have a problem, there are many blank lines, i don't understand the reason. If the result is not 0, then it should copy the matched string into the second array. Thanks a lot in advance for your Support. Link to comment Share on other sites More sharing options...
Floops Posted June 22, 2017 Share Posted June 22, 2017 (edited) You are using one counter variable for two arrays. This should work Func FileSearch1() $j = 0 For $i = 0 To UBound($content_array) - 1 $search_result=StringInStr($content_array[$i],$search) If $search_result<>0 Then $searchResultArray[$j] = $content_array[$i] $j += 1 EndIf Next EndFunc Edited June 22, 2017 by Floops Link to comment Share on other sites More sharing options...
LoneWolf_2106 Posted June 22, 2017 Author Share Posted June 22, 2017 i still see the empty strings, i guess it has something to do with the return of StringInStr Link to comment Share on other sites More sharing options...
Floops Posted June 22, 2017 Share Posted June 22, 2017 Can you post the text file where this happens? Link to comment Share on other sites More sharing options...
LoneWolf_2106 Posted June 22, 2017 Author Share Posted June 22, 2017 unfortunately i can't send the same file, but you give me the hint that there might be something in the file i am scanning. Indeed i tried with another file and it seems to be working. I will do some test. Link to comment Share on other sites More sharing options...
LoneWolf_2106 Posted June 22, 2017 Author Share Posted June 22, 2017 Unfortunately the problem is still there, i have made a test with a sample file. i've tried to search for the string "c9a50f22-7b4f-4eb8-8ac8-e01e0a441f0a". Together with this string, there are many other blank strings. UpdatesStore-20170614-035912.log Link to comment Share on other sites More sharing options...
Floops Posted June 22, 2017 Share Posted June 22, 2017 Weird, this works fine for me. How do you declare $searchResultArray? Link to comment Share on other sites More sharing options...
LoneWolf_2106 Posted June 22, 2017 Author Share Posted June 22, 2017 $searchResultArray[$Count], count is the rows count of the file. Link to comment Share on other sites More sharing options...
Floops Posted June 22, 2017 Share Posted June 22, 2017 Can you instead try declaring it as dim $searchResultArray[0] And using this as your function? Func FileSearch1() $j = 0 For $i = 0 To UBound($content_array) - 1 $search_result=StringInStr($content_array[$i],$search) If $search_result<>0 Then ReDim $searchResultArray[UBound($searchResultArray) + 1] $searchResultArray[$j] = $content_array[$i] $j += 1 EndIf Next EndFunc Link to comment Share on other sites More sharing options...
LoneWolf_2106 Posted June 22, 2017 Author Share Posted June 22, 2017 (edited) It has worked greatly , thank you very much! Now i understand the issue and how it works. I have read online that it was better to avoid the ReDim, now i am confused. Edited June 22, 2017 by LoneWolf_2106 Link to comment Share on other sites More sharing options...
mikell Posted June 22, 2017 Share Posted June 22, 2017 36 minutes ago, LoneWolf_2106 said: I have read online that it was better to avoid the ReDim, now i am confused. True, but don't be confused You can do it like this so you need to redim only once Local $searchResultArray[UBound($content_array)] Func FileSearch1() $j = 0 For $i = 0 To UBound($content_array) - 1 $search_result=StringInStr($content_array[$i], $search) If $search_result<>0 Then $searchResultArray[$j] = $content_array[$i] $j += 1 EndIf Next ReDim $searchResultArray[$j] EndFunc Link to comment Share on other sites More sharing options...
benners Posted June 22, 2017 Share Posted June 22, 2017 If you need to return an array of lines containing the search term, could you not use string split? #include <Array.au3> Local $as_Found = FileSearch1(@ScriptDir & '\UpdatesStore-20170614-035912.log', 'c9a50f22-7b4f-4eb8-8ac8-e01e0a441f0a') _ArrayDisplay($as_Found) Func FileSearch1($s_FilePath, $s_Search) Local $s_Found = '' Local $as_Content = FileReadToArray($s_FilePath) For $i = 0 to UBound($as_Content) - 1 If StringInStr($as_Content[$i], $s_Search) Then $s_Found &= '|' & $as_Content[$i] Next Return StringSplit(stringtrimleft($s_Found, 1), '|') EndFunc ;==>FileSearch1 Link to comment Share on other sites More sharing options...
LoneWolf_2106 Posted June 26, 2017 Author Share Posted June 26, 2017 Hi Benners, i didn't use it, because i am still new with AutoIT, i am learning now. Everyday i discover something new :-) On 22.06.2017 at 4:45 PM, benners said: If you need to return an array of lines containing the search term, could you not use string split? #include <Array.au3> Local $as_Found = FileSearch1(@ScriptDir & '\UpdatesStore-20170614-035912.log', 'c9a50f22-7b4f-4eb8-8ac8-e01e0a441f0a') _ArrayDisplay($as_Found) Func FileSearch1($s_FilePath, $s_Search) Local $s_Found = '' Local $as_Content = FileReadToArray($s_FilePath) For $i = 0 to UBound($as_Content) - 1 If StringInStr($as_Content[$i], $s_Search) Then $s_Found &= '|' & $as_Content[$i] Next Return StringSplit(stringtrimleft($s_Found, 1), '|') EndFunc ;==>FileSearch1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now