Imports System.Collections Imports System.Collections.Generic Imports System.Net Imports System.Text.RegularExpressions Module RankLanguagesByPopularity Private Class LanguageStat Public name As String Public count As Integer Public Sub New(name As String, count As Integer) Me.name = name Me.count = count End Sub End Class Private Function CompareLanguages(x As LanguageStat, y As languageStat) As Integer Dim result As Integer = 0 If x IsNot Nothing Or y IsNot Nothing Then If x Is Nothing Then result = -1 ElseIf y Is Nothing result = 1 Else result = - x.count.CompareTo(y.count) ' Sort descending count If result = 0 Then result = x.name.CompareTo(y.name) ' Sort ascending name End If End If End If Return result End Function Public Sub Main(ByVal args As String()) Dim languages As New List(Of LanguageStat) Dim basePage As String = "https://rosettacode.org/wiki/Category:Programming_Languages" Dim nextPage As String = basePage Dim nextPageEx = New RegEx("([^<]+?) * "" Dim wc As New WebClient() ' Ensure the WebClient has a User-Agent. Dim hasAgent As Boolean = False for hPos As Integer = 0 To wc.Headers.Count - 1 Dim headerKey As String = wc.Headers.GetKey(hPos) hasAgent = hasAgent Or headerKey = "User-Agent" Next hPos If not hasAgent Then wc.Headers.Add("User-Agent", "RC_Tasks_Agent") End If Dim page As String = wc.DownloadString(nextPage) nextPage = "" For Each link In nextPageEx.Matches(page) nextPage = basePage & link.Groups(1).Value Next link For Each language In languageStatEx.Matches(page) Dim lCount As Integer = 0 Dim lName As String = language.Groups(1).Value Dim countText As String = language.Groups(2).Value.Replace(",", "").Replace(".", "") If Not Integer.TryParse(countText, lCount) Console.Out.WriteLine(lName & "Invalid page count: <<" & countText & ">>") Else languages.Add(New LanguageStat(lName, lCount)) End If Next language If nextPage <> "" Then ' Sleep for a while to avoid looking like a DOS attack... System.Threading.Thread.Sleep(500) End If Loop languages.Sort(AddressOf CompareLanguages) Dim prevCount As Integer = -1 Dim prevPosition As Integer = -1 Dim position As Integer = 0 For Each stat As LanguageStat In languages position += 1 If stat.count <> prevCount Then prevPosition = position prevCount = stat.Count End If Console.Out.WriteLine(prevPosition.ToString.PadLeft(4) & ": " & stat.count.ToString.PadLeft(4) & " " & stat.name) Next stat End Sub End Module