Ace Master Posted March 9, 2004 Posted March 9, 2004 Hello. I have this txt file: ID NAME VALUE 10 NAME1 15 12 NAME2 30 16 NAME3 50 How to count lines in this file, but lines who start with id smaller then 15? in my example I need to return just 2 I know how to count lines but can't make it work with this. thanks Quote
wildfire1982 Posted March 9, 2004 Posted March 9, 2004 If you post your code I will have a look to see where you are going wrong. You need to read each line, adding to a counter if the substring of the beginning characters (parsed) <15 Quote Chris
Ace Master Posted March 9, 2004 Author Posted March 9, 2004 If you post your code I will have a look to see where you are going wrong. You need to read each line' date=' adding to a counter if the substring of the beginning characters (parsed) <15[/quote'] this is the code, a little complicated because for each loop the "name2" is different: Dim Read_status1 As New System.IO.StreamReader(nume2) Dim count As Integer = 0 For x1 = 0 To 80 Dim Line1 As String = Read_status1.ReadLine() If Line1 = "" Then Else Dim values_status1() = System.Text.RegularExpressions.Regex.Split(Line1, ControlChars.Tab) If values_status1(0) = "#ID" Then x1 = x1 - 1 Else count += 1 End If End If Next x1 Quote
sjn78 Posted March 9, 2004 Posted March 9, 2004 Dim Read_status1 As New System.IO.StreamReader(nume2) Dim count As Integer = 0 Dim Line1 As String = Read_status1.ReadLine() Do While Not Line1 Is Nothing Dim values_status1() = System.Text.RegularExpressions.Regex.Split(Line1, ControlChars.Tab) Try Dim val As Integer = CInt(values_status1(0)) If val < 15 Then count += 1 End If Catch End Try Line1 = Read_status1.ReadLine() Loop Read_status1.Close() Quote
Ace Master Posted March 10, 2004 Author Posted March 10, 2004 hi , and thanks for the answer sjn78 but....you code you give me isn't working because the values_status1(0) is "ID" and can't be converted to integer. another ideea is to skip the first line in counting, and then the values_status1(0) will be a int value and then will work, but don't know how to skip a line in a txt file counting. sugestions ? thanks Quote
sjn78 Posted March 10, 2004 Posted March 10, 2004 I know that "ID" wont go to an int, thats why its in a Try-Catch-Finally routine. When it sees this, it should just handle the error without crashing and go to the next line. I didn't try the code posted in a program, but I have done something like this before and it works. You may just have to play around with what I posted and it should work. Or you could just have Dim Line1 As String = Read_status1.ReadLine() Line1 = Read_status1.ReadLine() before the loop. It reads 2 lines, so your at the second line when the loop starts. Quote
Ace Master Posted March 11, 2004 Author Posted March 11, 2004 I know that "ID" wont go to an int, thats why its in a Try-Catch-Finally routine. When it sees this, it should just handle the error without crashing and go to the next line. I didn't try the code posted in a program, but I have done something like this before and it works. You may just have to play around with what I posted and it should work. Or you could just have Dim Line1 As String = Read_status1.ReadLine() Line1 = Read_status1.ReadLine() before the loop. It reads 2 lines, so your at the second line when the loop starts. is working now..thanks a lot. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.