Results 1 to 17 of 17

Thread: [RESOLVED] Crazy Counter

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Resolved [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:
    1. Dim reader As StreamReader = File.OpenText("file.txt")
    2. Dim line As String = reader.ReadLine()
    3. Dim a() As String
    4. Dim j As Integer
    5. Dim Tester As String
    6. Dim LoopCountA As String
    7. Dim LoopCountB aS Integer = 2
    8. While Not (line Is Nothing)
    9.   a = line.Split(",")
    10.   If LoopCountA <> a(1) Then
    11.     Tester = Tester & LoopCountB & ") "
    12.     Tester = Tester & a(1) & ","
    13.     Tester = Tester & a(2) & vbNewLine
    14.     LoopCountB = 1
    15.     LoopCountA = a(1)
    16.   Else
    17.     LoopCountB += 1
    18.   End If
    19.   line = reader.ReadLine()
    20. End While
    21. reader.Close()
    22. 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

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Crazy Counter

    What happens if you move vbNewLine?
    VB Code:
    1. Dim reader As StreamReader = File.OpenText("file.txt")
    2. Dim line As String = reader.ReadLine()
    3. Dim a() As String
    4. Dim j As Integer
    5. Dim Tester As String
    6. Dim LoopCountA As String
    7. Dim LoopCountB aS Integer = 2
    8. While Not (line Is Nothing)
    9.   a = line.Split(",")
    10.   If LoopCountA <> a(1) Then
    11.     If Tester <> Nothing then
    12.          Tester &= vbNewLine
    13.     End if
    14.     Tester &= TLoopCountB & ") "
    15.     Tester &= a(1) & ","
    16.     Tester &= a(2)
    17.     LoopCountB = 1
    18.     LoopCountA = a(1)
    19.   Else
    20.     LoopCountB += 1
    21.   End If
    22.   line = reader.ReadLine()
    23. End While
    24. reader.Close()
    25. mylabel.text = Tester

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    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?

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Crazy Counter

    Try this:
    VB Code:
    1. Dim sb As New System.Text.StringBuilder
    2.         Dim reader As IO.StreamReader = IO.File.OpenText("file.txt")
    3.         Dim sFileContents As String = reader.ReadToEnd
    4.         reader.Close()
    5.  
    6.         Dim arrLines() As String = System.Text.RegularExpressions.Regex.Split(sFileContents, "/r/n")
    7.  
    8.         For i As Int32 = 0 To arrLines.Length - 2
    9.             If arrLines(i).Split(","c)(0) <> arrLines(i + 1).Split(","c)(0) Then
    10.                 sb.Append(Environment.NewLine & (sb.Length - i) & ") " & arrLines(i))
    11.             End If
    12.         Next i
    13.  
    14.         mylabel.text = sb.ToString

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    Re: Crazy Counter

    That code prints out nothing, but a blank page wild_bill

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Crazy Counter

    Oops, forgot the regex options
    Quote Originally Posted by wild_bill
    Try this:
    VB Code:
    1. Dim sb As New System.Text.StringBuilder
    2.         Dim reader As IO.StreamReader = IO.File.OpenText("file.txt")
    3.         Dim sFileContents As String = reader.ReadToEnd
    4.         reader.Close()
    5.  
    6.         Dim arrLines() As String = System.Text.RegularExpressions.Regex.Split(sFileContents, "/r/n", RegexOptions.Singleline)
    7.  
    8.         For i As Int32 = 0 To arrLines.Length - 2
    9.             If arrLines(i).Split(","c)(0) <> arrLines(i + 1).Split(","c)(0) Then
    10.                 sb.Append(Environment.NewLine & (sb.Length - i) & ") " & arrLines(i))
    11.             End If
    12.         Next i
    13.  
    14.         mylabel.text = sb.ToString

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    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...

  8. #8
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Crazy Counter

    I'm being dumb today, try this:

    First add
    Imports System.Text.RegularExpressions
    VB Code:
    1. Dim arrLines() As String = Regex.Split(sFileContents, "\r\n",RegexOptions.Singleline)

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.

  10. #10
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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?

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    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

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.?

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    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?

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim groupList As New SortedList
    2.         Dim lineParts As String()
    3.         Dim reader As New IO.StreamReader("file name")
    4.  
    5.         While reader.Peek() <> -1
    6.             lineParts = reader.ReadLine().Split(","c)
    7.  
    8.             If Not groupList.ContainsKey(lineParts(1)) Then
    9.                 'Create a new list of numbers that correspond to the second number.
    10.                 groupList.Add(lineParts(1), New ArrayList)
    11.             End If
    12.  
    13.             'Add the last number to the list that corresponds to the second number.
    14.             DirectCast(groupList(lineParts(1)), ArrayList).Add(lineParts(2))
    15.         End While
    16.  
    17.         Dim tempStr As New System.Text.StringBuilder
    18.  
    19.         'Create the output string.
    20.         For Each groupNumber As String In groupList.GetKeyList()
    21.             If tempStr.Length > 0 Then
    22.                 tempStr.Append(Environment.NewLine)
    23.             End If
    24.  
    25.             tempStr.Append(DirectCast(groupList(groupNumber), ArrayList).Count)
    26.             tempStr.Append(") ")
    27.             tempStr.Append(groupNumber)
    28.             tempStr.Append(",")
    29.             tempStr.Append(DirectCast(groupList(groupNumber), ArrayList)(0))
    30.         Next
    31.  
    32.         MessageBox.Show(tempStr.ToString())

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Crazy Counter

    Let me make that a tiny bit more efficient:
    VB Code:
    1. Dim groupList As New SortedList
    2.         Dim groupSublist As ArrayList
    3.         Dim lineParts As String()
    4.         Dim reader As New IO.StreamReader("file name")
    5.  
    6.         While reader.Peek() <> -1
    7.             lineParts = reader.ReadLine().Split(","c)
    8.  
    9.             If groupList.ContainsKey(lineParts(1)) Then
    10.                 groupSublist = DirectCast(groupList(lineParts(1)), ArrayList)
    11.             Else
    12.                 'Create a new list of numbers that correspond to the second number.
    13.                 groupSublist = New ArrayList
    14.                 groupList.Add(lineParts(1), groupSublist)
    15.             End If
    16.  
    17.             'Add the last number to the list that corresponds to the second number.
    18.             groupSublist.Add(lineParts(2))
    19.         End While
    20.  
    21.         Dim tempStr As New System.Text.StringBuilder
    22.  
    23.         'Create the output string.
    24.         For Each groupNumber As String In groupList.GetKeyList()
    25.             If tempStr.Length > 0 Then
    26.                 tempStr.Append(Environment.NewLine)
    27.             End If
    28.  
    29.             groupSublist = DirectCast(groupList(groupNumber), ArrayList)
    30.             tempStr.Append(groupSublist.Count)
    31.             tempStr.Append(") ")
    32.             tempStr.Append(groupNumber)
    33.             tempStr.Append(",")
    34.             tempStr.Append(groupSublist(0))
    35.         Next
    36.  
    37.         MessageBox.Show(tempStr.ToString())
    Last edited by jmcilhinney; Jul 19th, 2005 at 06:20 AM.

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Location
    England
    Posts
    79

    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
  •  



Click Here to Expand Forum to Full Width