[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 248
  • Last Modified:

Getting values from a text file line by line

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
Mike Miller
Asked:
Mike Miller
  • 7
  • 3
  • 3
  • +1
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
line  = Streamreader.readLine()

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

else if line.Split(",").Count = 3 ' 2nd example
0
 
Mike MillerSoftware EngineerAuthor Commented:
What if there's 4 lines I need? Or 5? Or 20?
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nepaluzCommented:
what is the difference between the first and second sets of data to be able to differentiate which set we are looking at?
0
 
Mike MillerSoftware EngineerAuthor Commented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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
 
CodeCruiserCommented:
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
 
nepaluzCommented:
and what is this new permutation with 00221?
0
 
Mike MillerSoftware EngineerAuthor Commented:
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
 
Mike MillerSoftware EngineerAuthor Commented:
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
 
nepaluzCommented:
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
 
Mike MillerSoftware EngineerAuthor Commented:
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
 
Mike MillerSoftware EngineerAuthor Commented:
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
 
Mike MillerSoftware EngineerAuthor Commented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 7
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now