DragonHawk

ltrposhz-2.pl

Jan 24th, 2022 (edited)
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.34 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. # Count character position letter occurrence (frequency) in input.
  4. # First argument is length of word (# of letters) to look for.
  5. # Remaining arguments are input files, or stdin.
  6. # Assumes one word per input line, ASCII, English.
  7. # Excludes words containing non-letter characters (contractions like: can't)
  8. # Excludes words containing capital letters (proper names like: Alice)
  9. # Does not attempt to exclude plurals.
  10. # Was written for Wordle but has applications for other word games.
  11.  
  12. use strict;
  13. use warnings;
  14. use English;
  15. use feature qw( say );
  16.  
  17. # goldie = Goldilocks = just right (words the right length)
  18.  
  19. my $words = 0;   # count of words/lines seen
  20. my $goldies = 0; # count of words of correct length seen
  21. my @slot;        # letter position slots, each a hash, key=letter, val=count
  22. my $all = 0;     # slot zero counts occurrences in any/all positions
  23.  
  24. my $goldlen = shift;
  25. die "missing argument: word length" if not defined $goldlen;
  26. die "argument must be integer word length" if ($goldlen =~ m/[^0-9]/);
  27.  
  28.  WORD: while (<>) {
  29.  
  30.     chomp;
  31.     $words++;
  32.     next WORD if length != $goldlen;
  33.     next WORD if m/[^A-Za-z]/; # exclude impure words (non-letter chars)
  34.     next WORD if m/[A-Z]/; # exclude capital letters == proper names
  35.     $goldies++;
  36.  
  37.     # for each position slot, count that letter into the hash
  38.     my @letters = split //;
  39.     my $cur = 1; # starts at one, counts up for each LETTER
  40.     LETTER: foreach my $letter (@letters) {
  41.         $slot[$all]{$letter}++; # $all = zero for every LETTER
  42.         $slot[$cur]{$letter}++;
  43.         $cur++;
  44.         }
  45.     }
  46.  
  47. # output - totals
  48. say "words=$words goldies=$goldies";
  49.  
  50. # output - table
  51. # each row gives a letter and its counts
  52. # first field/column is the letter for that row
  53. # second column is the count for each letter in any slot
  54. # remaining columns are the counts for the letter in their slots
  55.  
  56. # column headers
  57. print "LTR\tALL\t";
  58. # column numbers, starting at one, as zero is "ALL"
  59. for (my $cur = 1; $cur <= $goldlen; $cur++) { print "$cur\t"; }
  60. print "\n";
  61.  
  62. # run through alphabet, and then each slot
  63. foreach my $letter ('a'..'z') {
  64.     print $letter, "\t";
  65.     # since @slot uses slot 0 for ALL, we can just iterate the array
  66.     for (my $cur = 0; $cur <= $goldlen; $cur++) {
  67.         my $count = $slot[$cur]{$letter};
  68.         $count = 0 if not defined $count;
  69.         print $count, "\t";
  70.         }
  71.     print "\n";
  72.     }
  73.  
  74. # EOF
  75.  
Add Comment
Please, Sign In to add comment