Rosetta Code/Rank languages by popularity: Difference between revisions
Content added Content deleted
(Updated the URLs. Added a test to extract the title as "Category:" may be missing. Updated the result.) |
(→{{header|C sharp|C#}}: Handle more that 500 languages and more than 999 tasks) |
||
Line 1,176: | Line 1,176: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Sorting only programming languages. |
Sorting only programming languages.<br> |
||
Note that this does not distinguish between tasks and non-task categories, so the counts can exceed the total number of tasks.<br> |
|||
⚫ | |||
Note this may need to set the Security Protocol type to Tls12 - see the section Problem running the C# entry in [[Talk:Rosetta_Code/Count_examples]] |
|||
⚫ | |||
using System; |
|||
using System.Collections; |
using System.Collections; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 1,188: | Line 1,191: | ||
static void Main(string[] args) |
static void Main(string[] args) |
||
{ |
{ |
||
string |
string get2 = new WebClient().DownloadString("http://www.rosettacode.org/w/index.php?" |
||
+"title=Special:Categories&limit=5000" |
|||
); |
|||
ArrayList langs = new ArrayList(); |
ArrayList langs = new ArrayList(); |
||
Dictionary<string, int> qtdmbr = new Dictionary<string, int>(); |
Dictionary<string, int> qtdmbr = new Dictionary<string, int>(); |
||
string cmcontinue = ""; |
|||
⚫ | |||
⚫ | |||
do |
|||
⚫ | |||
{ |
|||
string get1 = new WebClient().DownloadString("http://www.rosettacode.org/w/api.php?" |
|||
+"action=query&list=categorymembers" |
|||
+"&cmtitle=Category:Programming_Languages" |
|||
+"&cmlimit=500&format=json" |
|||
+cmcontinue |
|||
); |
|||
cmcontinue = ""; |
|||
⚫ | |||
MatchCollection cmcontinueMatch = new Regex("cmcontinue\":\"([a-z0-9A-Z|]*)\"").Matches(get1); |
|||
⚫ | |||
foreach (Match more in cmcontinueMatch) cmcontinue = "&cmcontinue=" + more.Groups[1].Value; |
|||
} |
|||
while( cmcontinue != "" ); |
|||
MatchCollection match2 = |
|||
⚫ | |||
foreach (Match match in match2) |
foreach (Match match in match2) |
||
Line 1,203: | Line 1,223: | ||
if (langs.Contains(match.Groups[1].Value)) |
if (langs.Contains(match.Groups[1].Value)) |
||
{ |
{ |
||
qtdmbr.Add(match.Groups[1].Value, Int32.Parse(match.Groups[2].Value |
qtdmbr.Add(match.Groups[1].Value, Int32.Parse(match.Groups[2].Value |
||
.Replace(",",string.Empty) |
|||
.Replace(".",string.Empty))); |
|||
} |
} |
||
} |
} |
||
string[] test = |
|||
qtdmbr.OrderByDescending(x => x.Value).Select(x => String.Format("{0,4} {1}", x.Value, x.Key)).ToArray(); |
|||
int count = 1; |
int count = 1; |
||
Line 1,213: | Line 1,236: | ||
foreach (string i in test) |
foreach (string i in test) |
||
{ |
{ |
||
Console.WriteLine("{0, |
Console.WriteLine("{0,4}: {1}", count, i); |
||
count++; |
count++; |
||
} |
} |
||
} |
} |
||
} |
|||
</syntaxhighlight> |
|||
{{out|Output (as of |
{{out|Output (as of September 11, 2023)}} |
||
1. 397 - Tcl |
|||
<pre> |
|||
⚫ | |||
1: 1660 Phix |
|||
2: 1653 Wren |
|||
3: 1617 Julia |
|||
4: 1598 Raku |
|||
5: 1577 Nim |
|||
6: 1548 Go |
|||
7: 1537 Perl |
|||
9. 290 - Common Lisp |
|||
⚫ | |||
10. 289 - AutoHotkey |
|||
9: 1376 J |
|||
10: 1287 C |
|||
. . . |
. . . |
||
</pre> |
|||
===Object-oriented solution=== |
===Object-oriented solution=== |
||
<syntaxhighlight lang="csharp">using System; |
<syntaxhighlight lang="csharp">using System; |