Rosetta Code/Rank languages by popularity: Difference between revisions

→‎{{header|C sharp|C#}}: Handle more that 500 languages and more than 999 tasks
(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:
 
=={{header|C sharp|C#}}==
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>
<syntaxhighlight lang="csharp">using System;
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]]
<syntaxhighlight lang="csharp">using System;
using System;
using System.Collections;
using System.Collections.Generic;
Line 1,188 ⟶ 1,191:
static void Main(string[] args)
{
string get1get2 = new WebClient().DownloadString("http://www.rosettacode.org/w/apiindex.php?action=query&list=categorymembers&cmtitle=Category:Programming_Languages&cmlimit=500&format=json");
string get2 = new WebClient().DownloadString( +"http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=5000");
);
 
ArrayList langs = new ArrayList();
Dictionary<string, int> qtdmbr = new Dictionary<string, int>();
 
string cmcontinue = "";
MatchCollection match1 = new Regex("\"title\":\"Category:(.+?)\"").Matches(get1);
MatchCollection match2 = new Regex("title=\"Category:(.+?)\">.+?</a>[^(]*\\((\\d+) members\\)").Matches(get2);
 
do
foreach (Match lang in match1) langs.Add(lang.Groups[1].Value);
{
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 match1languageMatch = new Regex("\"title\":\"Category:(.+?)\"").Matches(get1);
MatchCollection cmcontinueMatch = new Regex("cmcontinue\":\"([a-z0-9A-Z|]*)\"").Matches(get1);
foreach (Match lang in match1languageMatch) langs.Add(lang.Groups[1].Value);
foreach (Match more in cmcontinueMatch) cmcontinue = "&cmcontinue=" + more.Groups[1].Value;
}
while( cmcontinue != "" );
 
MatchCollection match2 =
MatchCollection match2 = new Regex("title=\"Category:(.+?)\">.+?</a>[^(]*\\(([\\d,.]+) members\\)").Matches(get2);
 
foreach (Match match in match2)
Line 1,203 ⟶ 1,223:
if (langs.Contains(match.Groups[1].Value))
{
qtdmbr.Add(match.Groups[1].Value, Int32.Parse(match.Groups[2].Value));
.Replace(",",string.Empty)
.Replace(".",string.Empty)));
}
}
 
string[] test =
string[] test = qtdmbr.OrderByDescending(x => x.Value).Select(x => String.Format("{0,34} - {1}", x.Value, x.Key)).ToArray();
 
int count = 1;
Line 1,213 ⟶ 1,236:
foreach (string i in test)
{
Console.WriteLine("{0,34}.: {1}", count, i);
count++;
}
}
}
}</syntaxhighlight>
{{out|Output (as of MaySeptember 3011, 20102023)}}
1. 397 - Tcl
<pre>
2. 368 - Python
3. 3501: -1660 RubyPhix
4. 3332: -1653 JWren
5. 3323: -1617 CJulia
6. 3224: -1598 HaskellRaku
7. 3225: -1577 OCamlNim
8. 3026: -1548 PerlGo
7: 1537 Perl
9. 290 - Common Lisp
2. 3688: -1487 Python
10. 289 - AutoHotkey
9: 1376 J
10: 1287 C
. . .
</pre>
 
===Object-oriented solution===
<syntaxhighlight lang="csharp">using System;
3,038

edits