Solved

vb.net count duplicate lines then sort from highest count to lowest

Posted on 2012-03-31
14
686 Views
Last Modified: 2012-08-13
Hello, I really need the following help in VB.Net. I am using VS 2008

1.) Read file c:\List.txt, which has a list of numbers per line.
2.) Need to count how many times a duplicate line item appears.
3.) Need to ouput that line item with count total to a file called c:\Amount.txt sorted by highest count to lowest count.

example of c:\list.txt -
80
92
78
64
92
59
64

example of c:\Amount.txt (sorted by count, highest to lowest) -
78 = 1
80 = 1
59 = 1
92 = 2
64 = 2

Could you please provide me with a complete coded solution for this problem in VB.Net

Thanks!
0
Comment
Question by:NeallyNeal
  • 6
  • 4
  • 4
14 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Using Linq you could do:

Module Module1

  Sub Main()
    Dim query = From line In System.IO.File.ReadAllLines("input.txt")
                Group By line Into Group
                Let c = Group.Count()
                Order By c
                Select New With {.Key = line, .Count = c}

    For Each item In query
      Console.WriteLine("{0} = {1}", item.Key, item.Count)
    Next

    Console.ReadKey()

  End Sub

End Module

Open in new window

0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 200 total points
Comment Utility
A derivative of the above code suggestion would be
Dim query = From line In System.IO.File.ReadAllLines("C:\List.txt")
     Group By line Into Group
     Let c = Group.Count()
     Order By c
     Select New With {.Key = line, .Count = c}

Using Writer As New System.IO.StreamWriter("C:\Amount.txt")
    For Each item In query
        Writer.WriteLine(item.Key & " = " & item.Count & vbCrLf)
    Next
End Using

Open in new window

0
 

Author Comment

by:NeallyNeal
Comment Utility
Hello and thank you for your speedy replies. The above mentioned could is producing the following listed errors below. Please keep in mind that I am using Visual Studio 2008. Could using the dictionary method be better? If so.., could you please supply a full coded solution using that method. Or help me to get your supplied code working? Thanks!

Error      1      'Group' is a type and cannot be used as an expression.      
Error      2      Method arguments must be enclosed in parentheses.      
Error      3      Name 'By' is not declared.      
Error      4      Comma, ')', or a valid expression continuation expected.      
Error      5      Name 'c' is not declared.      
Error      6      'Count' is not a member of 'System.Text.RegularExpressions.Group'.      
Error      7      Name 'Order' is not declared.      
Error      8      Method arguments must be enclosed in parentheses.      
Error      9      Name 'By' is not declared.      
Error      10      Comma, ')', or a valid expression continuation expected.      
Error      11      'Select Case' must end with a matching 'End Select'.      
Error      12      'Line' statements are no longer supported. File I/O functionality is available as 'Microsoft.VisualBasic.FileSystem.LineInput' and the graphics functionality is available as 'System.Drawing.Graphics.DrawLine'.      
Error      13      Name 'c' is not declared.      
Error      14      Statements and labels are not valid between 'Select Case' and first 'Case'.      
Error      15      Statements and labels are not valid between 'Select Case' and first 'Case'.      
Error      16      Statements and labels are not valid between 'Select Case' and first 'Case'.      
Error      17      'Key' is not a member of 'String'.      
Error      18      Statements and labels are not valid between 'Select Case' and first 'Case'.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I should have mentioned this prior, but you will need the following Imports statement:

Imports System.Linq
0
 

Author Comment

by:NeallyNeal
Comment Utility
Implemented import statement and nothing changed.
0
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
Here's a Non-LINQ (dictionary centric) code suggestion:
Dim TheDict As New SortedDictionary(Of Integer, Integer)
Using Reader As New System.IO.StreamReader("C:\List.txt")
    While Not Reader.EndOfStream
        Dim Line = Reader.ReadLine()
        If TheDict.ContainsKey(CInt(Line.Trim)) Then
            TheDict.Item(CInt(Line)) = +1
        Else
            TheDict.Add(CInt(Line), 1)
        End If
    End While
End Using
Using Writer As New System.IO.StreamWriter("C:\Amount.txt")
    For Each item In query
        Writer.WriteLine(item.Key & " = " & item.Count & vbCrLf)
    Next
End Using

Open in new window

0
 

Author Comment

by:NeallyNeal
Comment Utility
Error      1      Name 'query' is not declared.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
Hmmm!
        Dim TheDict As New SortedDictionary(Of Integer, Integer)
        Using Reader As New System.IO.StreamReader("C:\List.txt")
            While Not Reader.EndOfStream
                Dim Line = Reader.ReadLine()
                If TheDict.ContainsKey(CInt(Line.Trim)) Then
                    TheDict.Item(CInt(Line)) = +1
                Else
                    TheDict.Add(CInt(Line), 1)
                End If
            End While
        End Using
        Using Writer As New System.IO.StreamWriter("C:\Amount.txt")
            For Each x In TheDict.Keys
                Writer.WriteLine(x & " = " & TheDict.Item(x) & vbCrLf)
            Next
        End Using

Open in new window

0
 

Author Comment

by:NeallyNeal
Comment Utility
The following results are in the Amount.txt file:
59 = 1

64 = 1

78 = 1

80 = 1

92 = 1

The results that I'm looking for should be:
78 = 1
80 = 1
59 = 1
92 = 2
64 = 2

Thanks.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
Comment Utility
On my suggestion I forgot--since you are in 2008, you need to add the line continuation character ( _ ) to each line. For my first post, add a space-underscore to lines 4 - 7:

Module Module1

  Sub Main()
    Dim query = From line In System.IO.File.ReadAllLines("input.txt") _
                Group By line Into Group _
                Let c = Group.Count() _
                Order By c _
                Select New With {.Key = line, .Count = c}

    For Each item In query
      Console.WriteLine("{0} = {1}", item.Key, item.Count)
    Next

    Console.ReadKey()

  End Sub

End Module

Open in new window

0
 
LVL 17

Assisted Solution

by:nepaluz
nepaluz earned 200 total points
Comment Utility
I changed line 6 of the code suggestion
        Dim TheDict As New SortedDictionary(Of Integer, Integer)
        Using Reader As New System.IO.StreamReader("C:\List.txt")
            While Not Reader.EndOfStream
                Dim Line = Reader.ReadLine()
                If TheDict.ContainsKey(CInt(Line.Trim)) Then
                    TheDict.Item(CInt(Line)) = TheDict.Item(CInt(Line)) + 1
                Else
                    TheDict.Add(CInt(Line), 1)
                End If
            End While
        End Using
        Using Writer As New System.IO.StreamWriter("C:\Amount.txt")
            For Each x In TheDict.Keys
                Writer.WriteLine(x & " = " & TheDict.Item(x) & vbCrLf)
            Next
        End Using

Open in new window

0
 

Author Comment

by:NeallyNeal
Comment Utility
kaufmed and nepaluz, both revised codes are good. But I also need the results to be sorted by highest count to lowest count as originally requested.

Thank you so much...
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 300 total points
Comment Utility
You said "highest to lowest," but your example appears to be lowest to highest   = )

For mine, just change the order by:

...
Order By c Descending _
...

Open in new window

0
 

Author Closing Comment

by:NeallyNeal
Comment Utility
Everyone, thank you for your help. I decided to split and award the points based on the following:

1.)kaufmed - Recent solutions had sorted and counted my data as requested
2.)nepaluz - Solution showed me how to pipe the results from kaufmed solution to the file called Amount.txt, as I also requested.

Once again, thank each of you so much for your help on this one.
Thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now