Sign in to follow this  
Followers 0
Hypertrophy

get string in text file

13 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

 

Share this post


Link to post
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? :)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

What if JoeKing leaves the table and is no longer necessary to see what Seat he is in but he's still in the file? How do I get past this?

Share this post


Link to post
Share on other sites

bump

Share this post


Link to post
Share on other sites

What if JoeKing leaves the table and is no longer necessary to see what Seat he is in but he's still in the file? How do I get past this?

Look at my previous post :)

Share this post


Link to post
Share on other sites

im not familiar with StringRegExp but the example is just an example and player names may be switched around and such. It's very confusing :)

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

$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

Share this post


Link to post
Share on other sites

bump

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
Sign in to follow this  
Followers 0