Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-31
14
Medium Priority
?
750 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1200 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 800 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 1200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

715 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