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>
<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.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 get1 = new WebClient().DownloadString("http://www.rosettacode.org/w/api.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?"
string get2 = new WebClient().DownloadString("http://www.rosettacode.org/w/index.php?title=Special:Categories&limit=5000");
+"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 = "";
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 languageMatch = new Regex("\"title\":\"Category:(.+?)\"").Matches(get1);
MatchCollection cmcontinueMatch = new Regex("cmcontinue\":\"([a-z0-9A-Z|]*)\"").Matches(get1);
foreach (Match lang in languageMatch) langs.Add(lang.Groups[1].Value);
foreach (Match more in cmcontinueMatch) cmcontinue = "&cmcontinue=" + more.Groups[1].Value;
}
while( cmcontinue != "" );

MatchCollection match2 =
new Regex("title=\"Category:(.+?)\">.+?</a>[^(]*\\(([\\d,.]+) members\\)").Matches(get2);


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 =
string[] test = qtdmbr.OrderByDescending(x => x.Value).Select(x => String.Format("{0,3} - {1}", x.Value, x.Key)).ToArray();
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,3}. {1}", count, i);
Console.WriteLine("{0,4}: {1}", count, i);
count++;
count++;
}
}
}
}
}
}</syntaxhighlight>
</syntaxhighlight>
{{out|Output (as of May 30, 2010)}}
{{out|Output (as of September 11, 2023)}}
1. 397 - Tcl
<pre>
2. 368 - Python
3. 350 - Ruby
1: 1660 Phix
4. 333 - J
2: 1653 Wren
5. 332 - C
3: 1617 Julia
6. 322 - Haskell
4: 1598 Raku
7. 322 - OCaml
5: 1577 Nim
8. 302 - Perl
6: 1548 Go
7: 1537 Perl
9. 290 - Common Lisp
8: 1487 Python
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;