Solved

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

Posted on 2012-03-31
14
711 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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