LoneWolf_2106

Appending search results to the same array

6 posts in this topic

#1 ·  Posted

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

 

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

Not sure that I understand the problem. However, this line looks out of place

$j+=$counter

Why not set $j=$counter before the loop starts and just eliminate that line?

Share this post


Link to post
Share on other sites

#3 ·  Posted

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,
 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

#5 ·  Posted

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

 

Share this post


Link to post
Share on other sites

#6 ·  Posted

Hi,

i haven't tried your solutions yet, because i have restructered the code. As soon as i have time, i will try them and give you a feedback. Many thanks for your support.

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