Jump to content

Appending search results to the same array


Recommended Posts

Hi,

i have created a function which takes in Input an Array with a file Content(stored in an Array) and a string to search.

I have several files to scan and i am using the same Array to store my results.(which i have to print in a second stage)

I have created a global Counter out of the function to store the "Offset" and it should append the results of the successive files at the last Position of the Array.

But of course if i add the increment within the for Loop, the index will be spoiled because it will be doubled.

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

    Next
    Return $searchResultArray
EndFunc

 

May someone give me an advice to solve the issue?

Thanks in advance

 

 

Link to comment
Share on other sites

Try this function:
 

Func _ArraySearchToArray($sArrayToSearch, $stringToSearch, $nStart = 0)
    Local $rList, $rArray = UBound($sArrayToSearch) - 1
    If $nStart > $rArray Or $nStart < 0 Then Return SetError(1, 0, "")
    For $i = $nStart To $rArray
        If StringInStr($sArrayToSearch[$i], $stringToSearch) Then $rList &= $sArrayToSearch[$i] & @CR
    Next
    If $rList == "" Then Return SetError(2, 0, "")
    $rArray = StringSplit($rList, @CR, $nStart = 0 ? 2 : 0)
    Return $rArray
EndFunc   ;==>_ArraySearchToArray - Dao Van Trong - TRONG.WIN

 

Regards,
 

Link to comment
Share on other sites

If $search_result Then
    ReDim $searchResultArray[UBound($searchResultArray) + 1]
    $searchResultArray[UBound($searchResultArray)-1] = $file_content_array[$i]
EndIf

A little simplified

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Where jdelaney is going you don't need counters.

ditto.

Func FileSearch($file_content_array, $stringToSearch)
    For $i = 0 To UBound($file_content_array) - 1
        $search_result = StringInStr($file_content_array[$i], $stringToSearch)
        If $search_result Then ; Zero is false.   Anything that is not zero is true.
            ReDim $searchResultArray[UBound($searchResultArray) + 1]
            $searchResultArray[UBound($searchResultArray) - 1] = $file_content_array[$i] ; "UBound($searchResultArray) - 1" is the last index number of the array, $searchResultArray.
        EndIf
    Next
    Return $searchResultArray
EndFunc   ;==>FileSearch

 

Link to comment
Share on other sites

  • 3 weeks later...

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...