.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 24th, 2012, 03:34 AM
thequestor thequestor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 8 thequestor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 45 sec
Reputation Power: 0
To loop or not to loop that is the question

I'm currently working on learning VB.net and failing horribly.

I am creating a game combat log viewer and am stuck very early on [well early on the 3rd attempt at it] with a loop issue

the question is how would I find a bit of info in a line of text.

the code I am working on is
Code:
Public Function GetToonName(ByVal WhichToon)
        If WhichToon = 1 Then
            Dim ToonInfo As String = ReadSpecifiedLine(Label6.Text, 1)
            Dim str As String
            Dim strArr() As String
            Dim count As Integer
            str = ToonInfo
            strArr = str.Split(New Char() {"]"c})
            For count = 0 To strArr.Length - 1
                Try

                    Dim strValue
                    strValue = Replace(Replace(Regex.Replace(strArr(1), "\{.+?}]*?", ""), "@", ""), "[", "")
                    Label12.Text = strValue 'debug output
                    Label17.Text = Label12.Text
                    If Label7.Text = 0 Then
                        Label17.Text = "Loading"
                    End If
                Catch Ex As Exception
                    LogConsole.Text = Ex.Message
                End Try
            Next
        End If

        If WhichToon = 2 Then
            'this one is funky.
            'I need to check for a : and if it doesn't have one, read the next line, and if it doesn't have one the next  until it does
            'up to line 20 and then give up and list as no companion
            Dim ToonInfo As String = ReadSpecifiedLine(Label6.Text, 9 - 1)
            Dim str As String
            Dim strArr() As String
            Dim count As Integer
            str = ToonInfo
            strArr = str.Split(New Char() {"]"c})
            For count = 0 To strArr.Length - 1
                Try
                    LogConsole.Text = strArr(1)
                    Dim strValue
                    strValue = Replace(Replace(Regex.Replace(strArr(1), "\{.+?}]*?", ""), "@", ""), "[", "")
                    Label13.Text = Replace(strValue, Label12.Text + ":", "")  'debug output
                    Label16.Text = Label13.Text
                Catch Ex As Exception
                    LogConsole.Text = Ex.Message
                End Try
            Next
        End If
        ' - strAryWords is now an array

        Return 0
    End Function


the code I am using to read a specific line from the log file is

Code:
Public Shared Function ReadSpecifiedLine(file As String, lineNum As Integer) As String
        'create a variable to hold the contents of the file
        Dim contents As String = String.Empty
        'always use a try...catch to deal
        'with any exceptions that may occur
        Try
            Dim logFileStream As New FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
            Using stream As New StreamReader(logFileStream)
                contents = stream.ReadToEnd().Replace(vbCr & vbLf, vbLf).Replace(vbLf & vbCr, vbLf)
                Dim linesArray As String() = contents.Split(New Char() {ControlChars.Lf})

                'Make sure we have ana ctual array
                If linesArray.Length > 1 Then
                    'Make sure user didnt provide number greater than the number
                    'of lines in the array, and not less than 0 (zero) Thanks AdamSpeight2008
                    If Not lineNum > linesArray.Length AndAlso Not lineNum < 0 Then
                        Return linesArray(lineNum)
                    Else
                        'Failed our check so return the first line in the array
                        Return linesArray(0)
                    End If
                Else
                    'No array so return the line
                    Return contents
                End If
            End Using
        Catch Ex As Exception
            Return Ex.ToString()
        End Try
    End Function


and it works fine, per se.

Below is a sample log file I am reading from

Code:
[01:32:18.281] [@Flos'tok] [@Flos'tok] [Force Valor {875503313485824}] [ApplyEffect {836045448945477}: Force Valor {875503313485824}] ()
[01:32:18.281] [@Flos'tok] [@Flos'tok] [Force Valor {875503313485824}] [ApplyEffect {836045448945477}: Fortification {875503313486158}] ()
[01:32:18.282] [@Flos'tok] [@Flos'tok] [Force Valor {875503313485824}] [ApplyEffect {836045448945477}: Lucky Shots {875503313486145}] ()
[01:32:18.282] [@Flos'tok] [@Flos'tok] [Bio-Enhanced Resolve Stim {1483086567047168}] [ApplyEffect {836045448945477}: Bio-Enhanced Resolve Stim {1483086567047168}] ()
[01:32:18.282] [@Flos'tok] [@Flos'tok] [Safe Login {973870949466112}] [ApplyEffect {836045448945477}: Safe Login Immunity {973870949466372}] ()
[01:32:18.369] [@Flos'tok] [@Flos'tok] [] [Spend {836045448945473}: Force {836045448938502}] (100)
[01:32:18.369] [@Flos'tok] [@Flos'tok] [Combat Technique {979806594269184}] [ApplyEffect {836045448945477}: Combat Technique {979806594269184}] ()
[01:32:18.370] [@Flos'tok] [@Flos'tok] [Sprint {810670782152704}] [ApplyEffect {836045448945477}: Sprint {810670782152704}] ()
[01:32:19.967] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [ {2531161666486272}] [ApplyEffect {836045448945477}: Lucky Shots {2531161666486533}] ()
[01:32:19.967] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [ {2531161666486272}] [ApplyEffect {836045448945477}: Force Valor {2531161666486540}] ()
[01:32:19.968] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [ {2531161666486272}] [ApplyEffect {836045448945477}: Fortification {2531161666486543}] ()
[01:32:22.886] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [Med Scan {2074812801351680}] [Event {836045448945472}: AbilityActivate {836045448945479}] ()
[01:32:24.921] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [Med Scan {2074812801351680}] [ApplyEffect {836045448945477}: Heal {836045448945500}] (606) <45>
[01:32:26.030] [@Flos'tok] [@Flos'tok] [Safe Login {973870949466112}] [RemoveEffect {836045448945478}: Safe Login Immunity {973870949466372}] ()
[01:32:26.031] [@Flos'tok:Tharan Cedrax {493285583880192}] [@Flos'tok:Tharan Cedrax {493285583880192}] [Safe Login {973870949466112}] [RemoveEffect {836045448945478}: Safe Login Immunity {973870949466372}] ()
[01:32:38.426] [@Flos'tok] [@Flos'tok] [Looting {810795336204288}] [Event {836045448945472}: AbilityActivate {836045448945479}] ()
[01:32:59.413] [@Flos'tok] [@Flos'tok] [Deploy Field Repair Droid {2941953813512192}] [Event {836045448945472}: AbilityActivate {836045448945479}] ()
[01:33:00.966] [@Flos'tok] [@Flos'tok] [Deploy Field Repair Droid {2941953813512192}] [ApplyEffect {836045448945477}: Basic Field Repair Droid {2941953813512452}] ()



The 1st part [If WhichToon = 1 Then] works fine because the 1st line of the log file always returns the info I need for the primary player name

So when I use GetToonName(1) it gives me the proper info

NOW for the 2nd part [If WhichToon = 2 Then] I need to figure out how to read lines 2 through say 20

to look for [@ToonName:CompanionName and pull the companion name and assign that to say Label13.Text

I've tried things like
Code:
IF strArr(1).Contains(":") Then
....
End if


and it can see that if I tell it which line to look for

and I have tried to loop

Code:
 
IF NOT strArr(1).Contains(":") Then
Dim i

For i = 0 to 20
ToonInfo = ReadSpecifiedLine(Label6.Text, i)
Next
End if

and it does loop through it the specified # of times but i can't trap it when it DOES find the :

I'm stumped. Any help would be appreciated.

Reply With Quote
  #2  
Old July 24th, 2012, 09:36 AM
thequestor thequestor is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Posts: 8 thequestor User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 45 sec
Reputation Power: 0
Ok so I came up with a hack that does what I need

I ended up splitting it into 2 function, the 1st was pretty much what I had above the 2nd had to nest a couple loops but seems to work as I needed

Code:
Public Function GetToonName()
        Dim ToonInfo As String = ReadSpecifiedLine(Label6.Text, 1)
        Dim str As String
        Dim strArr() As String
        Dim count As Integer
        str = ToonInfo
        strArr = str.Split(New Char() {"]"c})
        For count = 0 To strArr.Length - 1
            Try
                Dim strValue
                strValue = Replace(Replace(Replace(Regex.Replace(strArr(1), "\{.+?}]*?", ""), "@", ""), "[", ""), " ", "")
                If Label16.Text = "Loaded" Then
                    Label17.Text = strValue
                End If
            Catch Ex As Exception
                LogConsole.Text = LogConsole.Text & "It ****ing errored " & Ex.Message
            End Try
        Next
        'Create Toons data dir
        Dim TLogDir As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\" & "Star Wars - The Old Republic\CombatLogs\ReaderFiles" & "\" & Label17.Text
        If Not My.Computer.FileSystem.DirectoryExists(TLogDir) Then
            My.Computer.FileSystem.CreateDirectory(TLogDir)
        End If

        Return 0
    End Function
    Public Function GetCompName()
        Dim strCompName As String = ""
        Dim logFileStream As New FileStream(Label6.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
        Try
            Using rdrFile As StreamReader = New StreamReader(logFileStream)
                Dim strCurrentLine As String = ""
                Do
                    strCurrentLine = rdrFile.ReadLine
                    'LogConsole.Text = LogConsole.Text & strCurrentLine
                    Dim TheCall = "@" & Label17.Text & ":"
                    'RichTextBox2.Text = TheCall
                    If strCurrentLine.Contains(TheCall) Then
                        'Store whole line into variable
                        strCompName = strCurrentLine

                        Exit Do
                    End If
                Loop Until strCurrentLine Is Nothing
            End Using 'rdrFile (closes file & disposes object)
        Catch Ex As Exception
            LogConsole.Text = Ex.Message
        End Try
        Dim TheCompLine = strCompName

        Dim str As String
        Dim strArr() As String
        Dim count As Integer
        str = TheCompLine
        strArr = str.Split(New Char() {"]"c})
        For count = 0 To strArr.Length - 1
            Try

                Dim strValue
                strValue = Replace(Replace(Replace(Regex.Replace(strArr(1), "\{.+?}]*?", ""), "@", ""), "[", ""), Label17.Text + ":", "")

                Label19.Text = strValue
            Catch Ex As Exception
                LogConsole.Text = Ex.Message
            End Try
        Next
        'Create Comp Data File
        Return 0
    End Function


It isn't pretty, but does seem to work.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > To loop or not to loop that is the question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap