Jump to content

Rosetta Code/Rank languages by popularity: Difference between revisions

→‎Perl 6: Using the API: Update to output sortable wikitables
(→‎Perl 6: Using the API: Update to output sortable wikitables)
Line 3,214:
use URI::Escape;
use JSON::Fast;
use Sort::Naturally;
 
my $client = HTTP::UserAgent.new;
Line 3,219 ⟶ 3,220:
my $url = 'http://rosettacode.org/mw';
 
my $start-timetablefile = now'./RC_Popularity.txt';
 
my %counts =
say "========= Generated: { DateTime.new(time) } =========";;
 
my $this = 1;
my $rank = 0;
my $last = 0;
my $tie = ' ';
 
.say for
mediawiki-query(
$url, 'pages',
Line 3,237 ⟶ 3,231:
:prop<categoryinfo>
)
.map({ .<title>.subst(/^'Category:'/, '') => .<categoryinfo><pages> || 0 });
 
my $out = open($tablefile, :w) or die "$!\n";
.map({ %( name => .<title>.subst(/^'Category:'/, ''),
count => .<categoryinfo><pages> || 0 ) })
 
# Add table boilerplate and header
.sort({ -.<count>, .<name> })
$out.say:
"\{|class=\"wikitable sortable\"\n",
"|+ As of { Date.today }\n",
"! Rank !! Language !! Count"
;
 
my @bg = <#fff; #ccc;>;
.map( { display(.<count>, .<name>) } );
my $ff = 0;
my $rank = 1;
my $ties = 0;
 
# Get sorted unique task counts
say "========= elapsed: {(now - $start-time).round(.01)} seconds =========";
for %counts.values.unique.sort: -* -> $count {
$ff++;
# Get list of tasks with this count
my @these = %counts.grep( *.value == $count )».keys.sort: *.&naturally;
 
for @these {
sub display ($count, $lang) {
if $last != $count {ties++;
$last = $count;out.say:
$rank = $this;"|-\n"~
"|style=\"background-color: { @bg[$ff % 2] }\"|$rank\n"~
$tie = ' '
"|style=\"background-color: { @bg[$ff % 2] }\"|[[:Category:$_|]]\n"~
} else {
"|style=\"background-color: { @bg[$ff % 2] }\"|$count";
$tie = 'T'
};
$rank += $ties;
sprintf "#%-4s Rank: %3d %s with %-4s %s: %s", $this++, $rank,
$ties = 0;
$tie, $count, ($count == 1 ?? 'entry ' !! 'entries'), $lang;
}
$out.say( "|}\n" );
$out.close;
 
sub mediawiki-query ($site, $type, *%query) {
Line 3,276 ⟶ 3,284:
}</lang>
 
{{out|Abridged output}} -See Firstfull andoutput last[[Rosetta_Code/Rank_languages_by_popularity/Full_list|here]]. 10 lines}}
<div style="height:50em;overflow:scroll;">
<pre>========= Generated: 2017-12-23T14:26:59Z =========
{|class="wikitable sortable"
#1 Rank: 1 with 958 entries: Racket
|+ As of 2018-05-18
#2 Rank: 2 with 943 entries: Python
! Rank !! Language !! Count
#3 Rank: 3 with 922 entries: Perl 6
|-
#4 Rank: 4 with 914 entries: Tcl
|style="background-color: #ccc;"|1
#5 Rank: 5 with 888 entries: Kotlin
|style="background-color: #ccc;"|[[:Category:Kotlin|Kotlin]]
#6 Rank: 6 with 880 entries: J
|style="background-color: #ccc;"|1006
#7 Rank: 7 with 872 entries: C
|-
#8 Rank: 8 with 858 entries: Zkl
|style="background-color: #fff;"|2
#9 Rank: 9 with 843 entries: Ruby
|style="background-color: #fff;"|[[:Category:Racket|Racket]]
#10 Rank: 10 with 824 entries: Go
|style="background-color: #fff;"|977
 
|-
...
|style="background-color: #ccc;"|3
 
|style="background-color: #ccc;"|[[:Category:Python|Python]]
#666 Rank: 589 T with 0 entries: UC++
|style="background-color: #ccc;"|960
#667 Rank: 589 T with 0 entries: UScript
|-
#668 Rank: 589 T with 0 entries: UserRPL
|style="background-color: #fff;"|4
#669 Rank: 589 T with 0 entries: VAX Assembly
|style="background-color: #fff;"|[[:Category:Perl 6|Perl 6]]
#670 Rank: 589 T with 0 entries: VRML
|style="background-color: #fff;"|945
#671 Rank: 589 T with 0 entries: WML
|-
#672 Rank: 589 T with 0 entries: WebAssembly
|style="background-color: #ccc;"|5
#673 Rank: 589 T with 0 entries: X10
|style="background-color: #ccc;"|[[:Category:Tcl|Tcl]]
#674 Rank: 589 T with 0 entries: XBase
|style="background-color: #ccc;"|914
#675 Rank: 589 T with 0 entries: XS
|-
#676 Rank: 589 T with 0 entries: ΜC++
|style="background-color: #fff;"|6
========= elapsed: 3.04 seconds =========
|style="background-color: #fff;"|[[:Category:C|C]]
</pre>
|style="background-color: #fff;"|896
|-
|style="background-color: #ccc;"|7
|style="background-color: #ccc;"|[[:Category:J|J]]
|style="background-color: #ccc;"|888
|-
|style="background-color: #fff;"|8
|style="background-color: #fff;"|[[:Category:Zkl|Zkl]]
|style="background-color: #fff;"|878
|-
|style="background-color: #ccc;"|9
|style="background-color: #ccc;"|[[:Category:Java|Java]]
|style="background-color: #ccc;"|867
|-
|style="background-color: #fff;"|10
|style="background-color: #fff;"|[[:Category:D|D]]
|style="background-color: #fff;"|857
|-
|style="background-color: #ccc;"|11
|style="background-color: #ccc;"|[[:Category:Ruby|Ruby]]
|style="background-color: #ccc;"|846
|-
|style="background-color: #fff;"|12
|style="background-color: #fff;"|[[:Category:Go|Go]]
|style="background-color: #fff;"|838
|-
|style="background-color: #ccc;"|13
|style="background-color: #ccc;"|[[:Category:Haskell|Haskell]]
|style="background-color: #ccc;"|824
|-
|style="background-color: #fff;"|14
|style="background-color: #fff;"|[[:Category:REXX|REXX]]
|style="background-color: #fff;"|823
|-
|style="background-color: #ccc;"|15
|style="background-color: #ccc;"|[[:Category:Perl|Perl]]
|style="background-color: #ccc;"|794
|-
|style="background-color: #fff;"|16
|style="background-color: #fff;"|[[:Category:Julia|Julia]]
|style="background-color: #fff;"|774
|-
|style="background-color: #ccc;"|17
|style="background-color: #ccc;"|[[:Category:PicoLisp|PicoLisp]]
|style="background-color: #ccc;"|768
|-
|style="background-color: #fff;"|18
|style="background-color: #fff;"|[[:Category:Sidef|Sidef]]
|style="background-color: #fff;"|763
|-
|style="background-color: #ccc;"|19
|style="background-color: #ccc;"|[[:Category:Mathematica|Mathematica]]
|style="background-color: #ccc;"|723
|-
|style="background-color: #fff;"|20
|style="background-color: #fff;"|[[:Category:Phix|Phix]]
|style="background-color: #fff;"|682
|}
</div>
 
===Perl 6: Using web scraping===
10,339

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.