Jump to content
rkr

Faster option for searching string

Recommended Posts

rkr

Hi, i am having a text file which has over 14000lines, i am using the following code (only part presented below) to search a string. the code is working, but this is taking lot of time, is there a better way to read and find the string

For $i= 0 to $tot_lines  ; $tot_lines is the total numbers of lines in file

      $read = filereadtoarray("file.txt")

      if $read[$i]=$search then    ; $search is defined as the string to search

          $line_found=$i

      endif 

next

  could some one please assist, thanks

 

 

Share this post


Link to post
Share on other sites
czardas

You place FileReadToArray() inside the loop: that's wrong in this case. Try this:

Local $aRead = FileReadToArray("file.txt")
Local $line_found = -1
Local $search = "hello world" ; Type your own search here term instead.

For $i = 0 to UBound($aRead) -1  ; UBound($aRead) -1 is the total numbers of lines in file
      If $aRead[$i] = $search Then    ; $search is defined as the string to search
          $line_found = $i
          ExitLoop ; unless you want to find all matches (requires a different approach)
      EndIf 
Next

If $line_found > -1 Then ConsoleWrite($aRead[$line_found] & @LF)

 

Edited by czardas

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

×