first_letter_top.pl 658 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 25 June 2016
  5. # Website: https://github.com/trizen
  6. # Make a top with the first letters of each word in a given text.
  7. # usage: cat file.txt | perl first_letter_top.pl
  8. use 5.014;
  9. use strict;
  10. use warnings;
  11. use List::Util qw(sum);
  12. use open IO => ':utf8', ':std';
  13. my %table;
  14. foreach my $word (split(' ', do { local $/; <> })) {
  15. if ($word =~ /^[^\pL]*(\pL)/) {
  16. $table{lc($1)}++;
  17. }
  18. }
  19. my $max = sum(values %table);
  20. foreach my $key (sort { $table{$b} <=> $table{$a} } keys %table) {
  21. printf("%s -> %3d (%5.2f%%)\n", $key, $table{$key}, $table{$key} / $max * 100);
  22. }