Jump to content

Return linenumber


Recommended Posts

Hi all,

I'm currently busy with making a client for a Online RPG. I was thinking for the following:

is it possible to search the whole textdocument (created by _IEBodyReadText()) for a specific word and then return the line number?

I have found this in the helpfile:

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Line read:", $line)
Wend

FileClose($file)

This reads the whole file and returns the lines if theres anything on it. I thought about using the StingInStr() function to search the line for a specific string, which is the right way in my opinion. But how to get the linenumber where the string is on to preform a filereadline() after the word has been found?

thanks!

Edited by PcExpert
Link to comment
Share on other sites

Maybe...

; demonstration to find chracters that change between to standard points
; or just find a string
#include <IE.au3>
#include <String.au3>

#Region --- IE-Builder generated code Start ---

$oIE = _IECreate()

;------------- User input --------------
_IENavigate($oIE, "http://www.autoitscript.com/") ; web address
$Find = "Welcome to the "  ; my info shows after this line... or just find this line
$Before = "- the home "     ; my info shows before this line... or set as ""
; ------------ End User input -------------
Sleep(1000)
$body = _IEBodyReadHTML($oIE)
$sloc = @TempDir & "\stest.txt" 
FileDelete($sloc)
FileWrite($sloc, $body)
$sfile = FileOpen($sloc, 0)
$num = 0
While 2
    $num = $num + 1
    $sline = FileReadLine($sfile, $num)
    If @error Then
        MsgBox(262208, "Fail", "The string was NOT found   ")
        FileClose($sfile)
        Exit
    EndIf
    If StringInStr($sline, $Find) Then
        MsgBox(64, "Success", "The string " & $Find & " was found    " & @CRLF & " on line # " & $num, 5)
        If $Before = "" Then ExitLoop
        $Found = _StringBetween($sline, $Find, $Before)
        MsgBox(64, "Found", "The string is *" & $Found[0] & "*    ", 5)
        ExitLoop
    EndIf
WEnd

#EndRegion --- IE-Builder generated code End ---

8)

NEWHeader1.png

Link to comment
Share on other sites

  • Moderators

PcExpert,

Have a look at this. It returns a value for the first line of the file in which the required text was found:

$text = "What you are looking for"

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$nLine = 0
; Read in lines of text until the EOF is reached
While 1
    $nLine += 1
    $line = FileReadLine($file)
    If @error = -1 Then
        $nLine = -1
        ExitLoop
    EndIf

    If StringInStr($line, $text) Then ExitLoop

Wend

If $nLine <> -1 Then MsgBox(0, $nLine, FileReadLine($file, $nLine))

FileClose($file)

But you already have the text of the line in $line - why do you need to read it again?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I need it because the RPG i'm making this client for (NOT a bot!) updates its site often, so the linenumbers where the text is on changes. So instead of using filereadline with a predifined linenumber i'd like to search for the string.

An example:

If I want to find this:

Money: 20,-

I currently use filereadline($MYFILENAME, $EXAMPLELINENUMBER) and after that, I use stringsplit() to make it only show the number, so its like this: '20'. But because of the reason mentioned earlier, I wanted to search for the word 'Money: " and then if its found, return the line number so I can use filereadline and stringsplit for further use. This way I dont have to change the predefined linenumbers to read. It saves me alot of work editing the code.

Edited by PcExpert
Link to comment
Share on other sites

  • Moderators

PcExpert,

So does what I posted do the job?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi all,

I'm currently busy with making a client for a Online RPG. I was thinking for the following:

is it possible to search the whole textdocument (created by _IEBodyReadText()) for a specific word and then return the line number?

I have found this in the helpfile:

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    MsgBox(0, "Line read:", $line)
Wend

FileClose($file)

This reads the whole file and returns the lines if theres anything on it. I thought about using the StingInStr() function to search the line for a specific string, which is the right way in my opinion. But how to get the linenumber where the string is on to preform a filereadline() after the word has been found?

thanks!

"Line number" is a fuzzy concept in HTML. Literal @LF and @CR are just ignored white space. Then entire document could be on one line of HTML code, but the "<br>" tags could still present a multi-line document.

Still, you could StringSplit() on @CR and then just search the resulting array, if you think the line number matters.

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Yes, it certainly does work now. Thanks M23 and PSaltyDS for your help. Works like a charm, tested the code for a few days now to see if it really works, but it just does.

Thanks again!

Edited by PcExpert
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...