Skip to content

Instantly share code, notes, and snippets.

@takdavid
Created May 10, 2018 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takdavid/bea4911f862e8d1ae964ac7376b069d8 to your computer and use it in GitHub Desktop.
Save takdavid/bea4911f862e8d1ae964ac7376b069d8 to your computer and use it in GitHub Desktop.
Word count, keeping all-caps word forms, with better ordering
#!/usr/bin/perl
binmode(STDIN, 'utf8');
binmode(STDOUT, 'utf8');
sub smart_lowercase($)
{
my $word = shift;
return $word if (length($word) > 1 and uc($word) eq $word);
return lc $word;
}
my %counter = ();
foreach (<STDIN>) { foreach (split /[^-\w]+/, $_) { $counter{smart_lowercase $_} += 1; } }
my @ordered = sort { $counter{$b} <=> $counter{$b} || $a <=> $b } keys %counter;
foreach (@ordered) { print "$counter{$_}\t$_\n"; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment