Solved

Getting values from a text file line by line

Posted on 2012-03-27
14
224 Views
Last Modified: 2012-03-30
I'm currently reading a text file. When I get to a line that contains
"00331," I grab the string following that text then go on to the next line.

Sometimes the lines will look like this...
00111,00445,dewmionw
00386,00331,ewqcweqcqw
00386,02343,ewqcweqcqw

and sometimes like this...
00111,00445,dewmionw
00386,00331,ewqcweqcqw
04386,00331,cwecweqcw
00356,00331,sakcPSKCASO
00111,00445,dewmionw
00386,02343,ewqcweqcqw

In the first example, I grab the value "ewqcweqcqw" and move on...no problem.
In the second example, however, I want to get all three values, then move on.

How do I do this. I've tried several loop variations, but I'm stuck.

TIA
0
Comment
Question by:mwmiller78
  • 7
  • 3
  • 3
  • +1
14 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
line  = Streamreader.readLine()

if line.Split(",").Count = 2 first example

else if line.Split(",").Count = 3 ' 2nd example
0
 

Author Comment

by:mwmiller78
Comment Utility
What if there's 4 lines I need? Or 5? Or 20?
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
what's the overall logic?

you can do a for each:


for each part as String in line.Split(",")

' do something with each part

next
0
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
what is the difference between the first and second sets of data to be able to differentiate which set we are looking at?
0
 

Author Comment

by:mwmiller78
Comment Utility
Basically I need to grab the final piece of information any time the line contains "00331"
I'm using this

                            strDurationTest = reader.ReadLine()
                            If strDurationTest.Contains("00331,") Then
                                strDurationTest = RemoveCoordinates(strDurationTest)
                            Else
                                strDurationTest = " "
                            End If
                            ListBox4.Items.Add(strDurationTest)

Open in new window


Which works great in the first set of data. I won't ever know how many instances of "00331" will exist though. I need to be able to do what I have above, look at the next line and do it again if Contains("00221,") = True but move on if not true.
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
Comment Utility
so set a boolean:

dim FirstValueFound as Boolean = false
                       if not firstValueFound then
                            If strDurationTest.Contains("00331,") Then
                                 strDurationTest = RemoveCoordinates(strDurationTest)
                                 FirstValueFound = true
                            Else
                                strDurationTest = " "
                            End If
                      else

                             if  strDurationTest.Contains("00221,") then
                                      'Do something
                            end if

                          firstValueFound = false
                      end if
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
Try this
Not sure why you would add an empty item to listbox. If you don't want to do that then move that line to inside the if condition

While reader.Peak() >= 0
    strDurationTest = reader.ReadLine()
    If strDurationTest.Contains("00331,") Then
       strDurationTest = RemoveCoordinates(strDurationTest)
    Else
       strDurationTest = " "
    End If
    ListBox4.Items.Add(strDurationTest)
End While

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
and what is this new permutation with 00221?
0
 

Author Comment

by:mwmiller78
Comment Utility
Cruiser...
I can't run through the rest of the file. I just need to see if the next line contains the specified value. If it does, repeat. If it does not, move on.
0
 

Author Comment

by:mwmiller78
Comment Utility
Actually if anyone can just tell me if there is a way to preview text in the next line or how to figure out what line I'm on and go back one, I think I'd be set.
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
Comment Utility
easiest way is read all lines into a list of string (or array if you are reading a large file) then you can move back and forth (even preview). Obviously, you have your preference for who provides a code suggestion, so I will leave it at that.
0
 

Author Comment

by:mwmiller78
Comment Utility
Here's an example of my text file

00484,2,484,:Satisfaisant/Satisfactory/Zufriedenstellend
00457,00124,Pressureandleaktests
00457,00215,/Epreuvesderésistanceetessaisd'étanchéité/
00457,00387,DruckundDichtheitsprüfung
00448,00117,Water/
00448,00143,Eau/
00448,00162,Wasser
00439,00134,Air/
00439,00147,Air/
00439,00159,Luft
00430,00106,Nitrogen/
00430,00143,Azote/
00430,00168,Stickstoff
00421,00140,Helium
00439,00204,Pressure(gauges)/
00430,00211,Pression/
00430,00247,Druck
00448,00294,Test
00439,00286,duration/
00430,00291,Durée/
00421,00286,Prüdauer
00443,00335,Resultconformto/
00434,00335,Résultatconformeà/
00425,00356,Ergebnis
00443,00417,AllowableLeakage
00434,00424,/
00434,00429,FuiteTolérée/
00425,00419,ZulassigeLeckage
00443,00500,ActualLeakage/
00443,00565,Fuite
00434,00502,Relevée/
00434,00538,Gemessene
00425,00527,Leckage
00404,00020,HydrostaticTest
00404,00106,W
00404,00106,mwmiller78
00404,00106,expertsexchange
00404,00204,2200PSI
00404,00279,2minute
00409,00331,ASMEB16-34/
00400,00331,EN12266-1
00404,00417,NONE
00404,00492,Satisfactory
00377,00020,SeatLeakTest
00377,00106,A
00377,00204,50PSI
00377,00279,N/A
00386,00331,CLASSVIANSI
00377,00331,FCI-70-2-2006/
00368,00331,EN1349
00377,00417,0.15scc/m
00377,00492,0.0scc/m
00356,8,356,Materialtestcertificates
00356,00113,/Certificatsmatières/
00356,00190,BescheinigungenüberWerkstoffprüfungen

Here is my code...

           Dim AssTestList As New List(Of String)
            Dim lineCollection As New Text.StringBuilder()
            Using reader As New System.IO.StreamReader(path)
                Dim foundStart As Boolean = False
                While Not reader.EndOfStream
                    Dim line2 As String = reader.ReadLine()
                    If line2.Contains("Pressureandleaktests") Then
                        'Pressure and Leak table exists
                        foundStart = True
                    ElseIf line2.Contains("Materialtestcertificates") AndAlso foundStart Then
                        Exit While
                    ElseIf foundStart Then
                        If line2.Contains("00020,") Then
                            ListBox1.Items.Add(line2.Substring(line2.LastIndexOf(",") + 1))
                            'AssTestList.Add(lineCollection.AppendLine(line2.Substring(line2.LastIndexOf(",") + 1)).ToString)






			    '************************************************************
                            strWaterTest = reader.ReadLine()
                            If strWaterTest.Contains("00106,") Then
                                strWaterTest = RemoveCoordinates(strWaterTest)
                            Else
                                strWaterTest = " "
                            End If
                            ListBox2.Items.Add(strWaterTest)
			    '************************************************************
                            


                           

                            intTableNumber = intTableNumber + 1
                        End If
                    End If
                End While
            End Using

Open in new window



This assigns the value "W" to strWaterTest.
I'm trying to get the value of strWaterTest (in this scenario) to be "W mwmiller78 expertsexchange"

In the above example there are three relevant values
W
mwmiller78
expertsexchange

This could be 1, it could be 5, it could be 50. I want to make one string (spaced) with all the  potential values.
0
 

Author Closing Comment

by:mwmiller78
Comment Utility
Ok I finally got it. It's freakin ugly but it does work. If anyone cares, here's the rough draft...

        Dim strFisherSpecTemp As String = ""
        Dim strFisherSpecTemp2 As String = ""
        Dim AssTestList As New List(Of String)
        Dim lineCollection As New Text.StringBuilder()
        Using reader As New System.IO.StreamReader(path)
            Dim foundStart As Boolean = False
            While Not reader.EndOfStream
                Dim line2 As String = reader.ReadLine()
                If line2.Contains("Gemessene") Then
                    foundStart = True
                ElseIf line2.Contains("Materialtestcertificates") AndAlso foundStart Then
                    Exit While
                ElseIf foundStart Then
                    If line2.Contains("00331,") Then
                        strFisherSpecTemp = line2.Substring(line2.LastIndexOf(",") + 1)
                        strFisherSpecTemp2 = "00331,"
                        Do While strFisherSpecTemp2.Contains("00331,") = True
                            strFisherSpecTemp2 = reader.ReadLine()
                            If strFisherSpecTemp2.Contains("00331,") = True Then
                                strFisherSpecTemp = strFisherSpecTemp & " " & RemoveCoordinates(strFisherSpecTemp2)
                            End If
                        Loop
                        strFisherSpecTemp2 = strFisherSpecTemp2.Replace("00331,", "")
                        ListBox5.Items.Add(strFisherSpecTemp)
                    End If
                End If
            End While
        End Using

Thanks for pointing me in the right direction nepaluz!
0
 

Author Comment

by:mwmiller78
Comment Utility
Oops...

Actually added an Exit Do as an Else to the last if statement, otherwise it loops through to EOF effectively voiding the initial While
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now