Advertisement
stackexchange-gilles

average-number-of-votes-on-all-sites/report

Nov 6th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.36 KB | None | 0 0
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Text::CSV;
  5.  
  6. my @language_sites = qw();
  7.  
  8. my $csv = Text::CSV->new;
  9. my $top_row = $csv->getline(*STDIN);
  10. my @rows = ();
  11. while (my $row = $csv->getline(*STDIN)) {
  12.     $row->[0] =~ s/^StackExchange\.(.*)/lc($1)/e;
  13.     push @rows, $row;
  14. }
  15.  
  16. sub report {
  17.     my ($include_top, $include_bottom, $include_median, @sites) = @_;
  18.     print "                    up    down\n";
  19.     for (my $rank = 0; $rank < @rows; $rank++) {
  20.         my @row = @{$rows[$rank]};
  21.         my $site = $row[0];
  22.         printf "%-18s %5.2f %5.2f\n", $site, @row[4,5]
  23.           if $rank < $include_top
  24.           or $include_median && $rank == @rows / 2
  25.           or $rank >= @rows - $include_bottom
  26.           or grep {$_ eq $site} @sites;
  27.     }
  28. }
  29.  
  30. if (@ARGV == 0) {
  31.     report(scalar(@rows), 0, ());
  32. } elsif ($ARGV[0] eq '--help') {
  33.     print <<EOF;
  34. Usage: $0 [STYLE]
  35. Show the average number of upvotes and downvotes per post on Stack Exchange
  36. sites. Deleted posts are excluded.
  37.  
  38. This script reads CSV data on standard input. The data can be retrieved from
  39. http://data.stackexchange.com/stackoverflow/query/195762/average-number-of-votes-on-all-sites
  40. EOF
  41. } elsif ($ARGV[0] eq 'cs') {
  42.     report(6, 1, 1, qw(astronomy biology chemistry cogsci crypto cs cstheory
  43.                        earthscience linguistics
  44.                        math matheducators mathoverflow
  45.                        philosphy physics programmers
  46.                        scicomp stackoverflow stats tex));
  47. } elsif ($ARGV[0] eq 'it') {
  48.     report(1, 1, 1, qw(android apple arduino blender codegolf codereview
  49.                        dba drupal dsp ebooks electronics expressionengine
  50.                        gamedev gis joomla magento mathematica
  51.                        networkengineering programmers
  52.                        raspberrypi reverseengineering salesforce
  53.                        security serverfault sharepoint stackoverflow superuser
  54.                        tex tor tridion ubuntu unix ux
  55.                        webapps webmasters windowsphone wordpress));
  56. } elsif ($ARGV[0] eq 'lang') {
  57.     report(1, 1, 1, qw(chinese ell english french german italian
  58.                        japanese linguistics russian spanish writers));
  59. } elsif ($ARGV[0] =~ /[[:space:[,]/) {
  60.     report(0, 0, 0, split(/[[:space:],]+/, $ARGV[0]));
  61. } else {
  62.     die "Unknown report style";
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement