Jump to content

StringInStr result


Recommended Posts

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

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
Link to comment
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

 

Link to comment
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

 

Link to comment
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

 

Link to comment
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

 

 

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...