LoneWolf_2106

StringInStr result

13 posts in this topic

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.

Share this post


Link to post
Share on other sites

#2 ·  Posted (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 by Floops

Share this post


Link to post
Share on other sites

i still see the empty strings, i guess it has something to do with the return of StringInStr

Share this post


Link to post
Share on other sites

Can you post the text file where this happens?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Weird, this works fine for me. How do you declare $searchResultArray?

Share this post


Link to post
Share on other sites

$searchResultArray[$Count], count is the rows count of the file. 

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (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 by LoneWolf_2106

Share this post


Link to post
Share on other sites
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

 

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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

 

 

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