Jump to content

get string in text file


Recommended Posts

hi, very hard problem i have encountered. im trying to read if JoeKing is located in a text file (this i can do), but if string exists then look for JoeKing again and this time look for a string preceding JoeKing. for example lets say my text file looks like the one below

Seat 1: texas14 (1,000)
Seat 3: JoeKing (0)
Seat 5: Red1983 (960)
Seat 6: HOOMS (580)
Seat 7: D415 (1,625)
Seat 8: ded (7,133)
Seat 9: Pit3 (1,650)
JoeKing says hello
ded1966 is leaving

after I check if JoeKing is in the text file how can i check what seat he is in?

Edited by Hypertrophy
Link to comment
Share on other sites

hi, very hard problem i have encountered. im trying to read if JoeKing is located in a text file (this i can do), but if string exists then look for JoeKing again and this time look for a string preceding JoeKing. for example lets say my text file looks like the one below

Seat 1: texas14 (1,000)
Seat 3: JoeKing (0)
Seat 5: Red1983 (960)
Seat 6: HOOMS (580)
Seat 7: D415 (1,625)
Seat 8: ded (7,133)
Seat 9: Pit3 (1,650)
JoeKing says hello
ded1966 is leaving

after I check if JoeKing is in the text file how can i check what seat he is in?

You using StringRegExp? If so... just do another check...? o.o

If $joe = true Then

StringRegExp($seat)

EndIf

[As example use]

Or maybe I'm missing what your asking.... lol xD

Link to comment
Share on other sites

If I understand your problem correct you are looking for the "JoeKing says hello" (or similar) line. If found you would like to get the "Seat 3: JoeKing (0)" line?

#Include <File.au3>
Global $asArray[1]
_FileReadToArray("C:\temp\test.txt",$asArray)
For $i = 1 To $asArray[0]
    If StringInStr($asArray[$i],"JoeKing") > 0 Then
        If StringLeft($asArray[$i],5) = "Seat " Then 
            $Seat = StringMid($asArray[$i],6,1)
        Else
            MsgBox(0,"","JoeKing has Seat :" & $Seat)
        EndIf
    EndIf
Next

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

#include <Array.au3>
#include <file.au3>
Dim $aRecords
_FileReadToArray("test.txt",$aRecords)
$sSearch = "JoeKing"
$iIndex = _ArraySearch($aRecords, $sSearch, 0, 0, 0, 1)
If @error Then
    MsgBox(0, "Not Found", '"' & $sSearch & '" was not found in the array.')
Else
    MsgBox(0, $aRecords[$iIndex], '"' & $sSearch & '" was found in the array at position ' & $iIndex & ".")
    $iIndex1 = _ArraySearch($aRecords, $sSearch, $iIndex+1, 0, 0, 1)
    If @error Then
        MsgBox(0, "Not Found", '"' & $sSearch & '" was not found in the array.')
    Else
        MsgBox(0, $aRecords[$iIndex1], '"' & $sSearch & '" was found in the array at position ' & $iIndex1 & ".")
    EndIf
EndIf

maby something like this :)

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

If I understand your problem correct you are looking for the "JoeKing says hello" (or similar) line. If found you would like to get the "Seat 3: JoeKing (0)" line?

#Include <File.au3>
Global $asArray[1]
_FileReadToArray("C:\temp\test.txt",$asArray)
For $i = 1 To $asArray[0]
    If StringInStr($asArray[$i],"JoeKing") > 0 Then
        If StringLeft($asArray[$i],5) = "Seat " Then 
            $Seat = StringMid($asArray[$i],6,1)
        Else
            MsgBox(0,"","JoeKing has Seat :" & $Seat)
        EndIf
    EndIf
Next

wow! this is amazing! thank you so much for help. question though. before i was using FileRead() to read the text file, using _FileReadToArray seems so much better, would it mess things up to use it instead? :)

Link to comment
Share on other sites

$sFile = @ScriptDir & "\test.$$$"
If FileExists($sFile) Then FileDelete($sFile)
FileWrite($sFile, "Seat 1: texas14 (1,000)" & @CRLF & "Seat 3: JoeKing (0)" _
    & @CRLF & "Seat 5: Red1983 (960)" & @CRLF & "Seat 6: HOOMS (580)" & @CRLF & "Seat 7: D415 (1,625)" _
    & @CRLF & "Seat 8: ded (7,133)" & @CRLF & "Seat 9: Pit3 (1,650)" & @CRLF & "JoeKing says hello" _
    & @CRLF & "ded1966 is leaving")

$sSearch = "JoeKing"

$aRet = StringRegExp(FileRead($sFile), '(?i)(seat *\d*\: *)' & $sSearch, 3)
If Not @error Then
    For $i = 0 To UBound($aRet) -1
        ConsoleWrite($sSearch & " in " & $aRet[$i] & @LF)
    Next
Else
    ConsoleWrite($sSearch & " not found" & @LF)
EndIf

Link to comment
Share on other sites

wow! this is amazing! thank you so much for help. question though. before i was using FileRead() to read the text file, using _FileReadToArray seems so much better, would it mess things up to use it instead? :)

Using an array should not mess things up.

Here is a non-array approach for your consideration.

;
Local $sSearchName = "JoeKing"
Local $aRes = StringRegExp(FileRead(@ScriptDir & "\text.txt"), "Seat (.+): " & $sSearchName, 3)

If IsArray($aRes) Then
    MsgBox(0, "", $sSearchName & " at Seat " & $aRes[0])
Else
    MsgBox(0, "", $sSearchName & " not found.")
EndIf
;

In the regular expression pattern after Seat there is (.+) this allows for a seat labeled AB-1,234. (\d+) could be used if the seats are digits only.

Edit: picaxe

Yours is better in that it would show multiple ocurrences of the search name, which is a possibility.

Edited by Malkey
Link to comment
Share on other sites

$sFile = @ScriptDir & "\test.$$$"
If FileExists($sFile) Then FileDelete($sFile)
FileWrite($sFile, "Seat 1: texas14 (1,000)" & @CRLF & "Seat 3: JoeKing (0)" _
    & @CRLF & "Seat 5: Red1983 (960)" & @CRLF & "Seat 6: HOOMS (580)" & @CRLF & "Seat 7: D415 (1,625)" _
    & @CRLF & "Seat 8: ded (7,133)" & @CRLF & "Seat 9: Pit3 (1,650)" & @CRLF & "JoeKing says hello" _
    & @CRLF & "ded1966 is leaving")

$sSearch = "JoeKing"

$aRet = StringRegExp(FileRead($sFile), '(?i)(seat *\d*\: *)' & $sSearch, 3)
If Not @error Then
    For $i = 0 To UBound($aRet) -1
        ConsoleWrite($sSearch & " in " & $aRet[$i] & @LF)
    Next
Else
    ConsoleWrite($sSearch & " not found" & @LF)
EndIf

I see how you wrote to another file the exact data I posted. I would replace it with the handle to the actual file's data that im reading from right? Because my OP was just an example...

Edit: What if he leaves though? I don't understand how your script would know. Is there a way to just read at the end of the file? The file I'm reading is CONSTANTLY growing so how could it read the whole thing and then from there just keep reading from wherever it left off to prevent old data from messing up things?

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