Solved

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

Posted on 2012-03-31
14
718 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 75

Expert Comment

by:käµfm³d 👽
ID: 37792461
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
ID: 37792567
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
ID: 37793065
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
LVL 75

Expert Comment

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

Imports System.Linq
0
 

Author Comment

by:NeallyNeal
ID: 37793103
Implemented import statement and nothing changed.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37793134
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
ID: 37793141
Error      1      Name 'query' is not declared.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37793155
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
ID: 37793168
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 75

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 37793179
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
ID: 37793194
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
ID: 37793232
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 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 300 total points
ID: 37793240
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
ID: 37793254
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 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