Solved

Getting values from a text file line by line

Posted on 2012-03-27
14
234 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:Mike Miller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 3
  • +1
14 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37772978
line  = Streamreader.readLine()

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

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

Author Comment

by:Mike Miller
ID: 37772989
What if there's 4 lines I need? Or 5? Or 20?
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37773257
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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 17

Expert Comment

by:nepaluz
ID: 37773632
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:Mike Miller
ID: 37773830
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 40

Expert Comment

by:Kyle Abrahams
ID: 37773843
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
ID: 37773998
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
 
LVL 17

Expert Comment

by:nepaluz
ID: 37774057
and what is this new permutation with 00221?
0
 

Author Comment

by:Mike Miller
ID: 37777836
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:Mike Miller
ID: 37777864
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
ID: 37778234
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:Mike Miller
ID: 37778887
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:Mike Miller
ID: 37788099
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:Mike Miller
ID: 37788324
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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