|
-
Jul 18th, 2005, 09:27 AM
#1
Thread Starter
Lively Member
[RESOLVED] Crazy Counter
I have a CSV file that im reading in, which has 3 fields and un-known lines, as it is constantly updated.
Now Im looking to add a counter, and grouping system...
Here is the code I have thus far:
VB Code:
Dim reader As StreamReader = File.OpenText("file.txt")
Dim line As String = reader.ReadLine()
Dim a() As String
Dim j As Integer
Dim Tester As String
Dim LoopCountA As String
Dim LoopCountB aS Integer = 2
While Not (line Is Nothing)
a = line.Split(",")
If LoopCountA <> a(1) Then
Tester = Tester & LoopCountB & ") "
Tester = Tester & a(1) & ","
Tester = Tester & a(2) & vbNewLine
LoopCountB = 1
LoopCountA = a(1)
Else
LoopCountB += 1
End If
line = reader.ReadLine()
End While
reader.Close()
mylabel.text = Tester
Now this sort of works... except for some reason i puts the highest number at the bottom of the list instead of next to the correct numbers. Any reason why? Anyone able to help me out?
Many thanks
-
Jul 18th, 2005, 10:14 AM
#2
Re: Crazy Counter
What happens if you move vbNewLine?
VB Code:
Dim reader As StreamReader = File.OpenText("file.txt")
Dim line As String = reader.ReadLine()
Dim a() As String
Dim j As Integer
Dim Tester As String
Dim LoopCountA As String
Dim LoopCountB aS Integer = 2
While Not (line Is Nothing)
a = line.Split(",")
If LoopCountA <> a(1) Then
If Tester <> Nothing then
Tester &= vbNewLine
End if
Tester &= TLoopCountB & ") "
Tester &= a(1) & ","
Tester &= a(2)
LoopCountB = 1
LoopCountA = a(1)
Else
LoopCountB += 1
End If
line = reader.ReadLine()
End While
reader.Close()
mylabel.text = Tester
-
Jul 18th, 2005, 10:25 AM
#3
Thread Starter
Lively Member
Re: Crazy Counter
Still getting problems...
EG of what comes out:
1) 68,285
3) 69,291
What should be coming out:
3) 69,285
1) 69,291
Any other ideas?
-
Jul 18th, 2005, 11:02 AM
#4
Re: Crazy Counter
Try this:
VB Code:
Dim sb As New System.Text.StringBuilder
Dim reader As IO.StreamReader = IO.File.OpenText("file.txt")
Dim sFileContents As String = reader.ReadToEnd
reader.Close()
Dim arrLines() As String = System.Text.RegularExpressions.Regex.Split(sFileContents, "/r/n")
For i As Int32 = 0 To arrLines.Length - 2
If arrLines(i).Split(","c)(0) <> arrLines(i + 1).Split(","c)(0) Then
sb.Append(Environment.NewLine & (sb.Length - i) & ") " & arrLines(i))
End If
Next i
mylabel.text = sb.ToString
-
Jul 18th, 2005, 11:10 AM
#5
Thread Starter
Lively Member
Re: Crazy Counter
That code prints out nothing, but a blank page wild_bill
-
Jul 18th, 2005, 11:13 AM
#6
Re: Crazy Counter
Oops, forgot the regex options
 Originally Posted by wild_bill
Try this:
VB Code:
Dim sb As New System.Text.StringBuilder
Dim reader As IO.StreamReader = IO.File.OpenText("file.txt")
Dim sFileContents As String = reader.ReadToEnd
reader.Close()
Dim arrLines() As String = System.Text.RegularExpressions.Regex.Split(sFileContents, "/r/n", RegexOptions.Singleline)
For i As Int32 = 0 To arrLines.Length - 2
If arrLines(i).Split(","c)(0) <> arrLines(i + 1).Split(","c)(0) Then
sb.Append(Environment.NewLine & (sb.Length - i) & ") " & arrLines(i))
End If
Next i
mylabel.text = sb.ToString
-
Jul 18th, 2005, 11:16 AM
#7
Thread Starter
Lively Member
Re: Crazy Counter
It's still giving me a blank, but also the RegexOptions bit of code is underlined in blue as if VB.NET doesn't like it, or think that it is valid...
-
Jul 18th, 2005, 11:57 AM
#8
Re: Crazy Counter
I'm being dumb today, try this:
First add
Imports System.Text.RegularExpressions
VB Code:
Dim arrLines() As String = Regex.Split(sFileContents, "\r\n",RegexOptions.Singleline)
-
Jul 18th, 2005, 10:52 PM
#9
Re: Crazy Counter
Given that this file is of indeterminate length, I would suggest using a StringBuilder and calling Append rather than concatenating all those Strings. This has nothing really to do with your problem but it is the preferred way to deal with this many string manipulations. You could also set the initial capacity of the StringBuilder based on the size of the file. This would avoid having to reallocate memory each time the StringBuilder reached its capacity.
-
Jul 19th, 2005, 01:51 AM
#10
Re: Crazy Counter
Is there a rule of thumb when to use a stringbuilder rather than concatenating a string? How can I set the initial capacity of a string builder?
-
Jul 19th, 2005, 02:07 AM
#11
Re: Crazy Counter
You can set the initial capacity of a StringBuilder using the constructor and you can set the Capacity property at any time. There is a rule of thumb regarding string manipulation and StringBuilders. I got my information here, about half-way down. This is an MSDN article so it should be relatively reliable you would think, but other articles may have slightly different things to say perhaps.
-
Jul 19th, 2005, 03:47 AM
#12
Thread Starter
Lively Member
Re: Crazy Counter
Still giving me a blank page wild_bill...
I think I may just go back to my code, but still I don't know why it's grouping them in-correctly
-
Jul 19th, 2005, 04:19 AM
#13
Re: Crazy Counter
Could you show an example of the content of the data file and the exact code you are currently using and what you expect the output to look like.?
-
Jul 19th, 2005, 05:39 AM
#14
Thread Starter
Lively Member
Re: Crazy Counter
Example contents:
0,68,284
0,68,285
0,68,286
0,69,291
It should come up like this:
3) 68,286
1) 69,291
I don't really care what the last number is to be honest, as I am just doing this to give me the ability to group by the two digit numbers, so that I can handle them seperately...
Any ideas?
-
Jul 19th, 2005, 06:01 AM
#15
Re: Crazy Counter
You will need to make a few minor adjustments to this code, including the fact that you may want to convert the strings to integers.
VB Code:
Dim groupList As New SortedList
Dim lineParts As String()
Dim reader As New IO.StreamReader("file name")
While reader.Peek() <> -1
lineParts = reader.ReadLine().Split(","c)
If Not groupList.ContainsKey(lineParts(1)) Then
'Create a new list of numbers that correspond to the second number.
groupList.Add(lineParts(1), New ArrayList)
End If
'Add the last number to the list that corresponds to the second number.
DirectCast(groupList(lineParts(1)), ArrayList).Add(lineParts(2))
End While
Dim tempStr As New System.Text.StringBuilder
'Create the output string.
For Each groupNumber As String In groupList.GetKeyList()
If tempStr.Length > 0 Then
tempStr.Append(Environment.NewLine)
End If
tempStr.Append(DirectCast(groupList(groupNumber), ArrayList).Count)
tempStr.Append(") ")
tempStr.Append(groupNumber)
tempStr.Append(",")
tempStr.Append(DirectCast(groupList(groupNumber), ArrayList)(0))
Next
MessageBox.Show(tempStr.ToString())
-
Jul 19th, 2005, 06:07 AM
#16
Re: Crazy Counter
Let me make that a tiny bit more efficient:
VB Code:
Dim groupList As New SortedList
Dim groupSublist As ArrayList
Dim lineParts As String()
Dim reader As New IO.StreamReader("file name")
While reader.Peek() <> -1
lineParts = reader.ReadLine().Split(","c)
If groupList.ContainsKey(lineParts(1)) Then
groupSublist = DirectCast(groupList(lineParts(1)), ArrayList)
Else
'Create a new list of numbers that correspond to the second number.
groupSublist = New ArrayList
groupList.Add(lineParts(1), groupSublist)
End If
'Add the last number to the list that corresponds to the second number.
groupSublist.Add(lineParts(2))
End While
Dim tempStr As New System.Text.StringBuilder
'Create the output string.
For Each groupNumber As String In groupList.GetKeyList()
If tempStr.Length > 0 Then
tempStr.Append(Environment.NewLine)
End If
groupSublist = DirectCast(groupList(groupNumber), ArrayList)
tempStr.Append(groupSublist.Count)
tempStr.Append(") ")
tempStr.Append(groupNumber)
tempStr.Append(",")
tempStr.Append(groupSublist(0))
Next
MessageBox.Show(tempStr.ToString())
Last edited by jmcilhinney; Jul 19th, 2005 at 06:20 AM.
-
Jul 19th, 2005, 06:15 AM
#17
Thread Starter
Lively Member
Re: Crazy Counter
And now the code works 
Cheers man.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|