diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-03-07 08:27:30 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-07 08:27:30 +0000 |
| commit | fe1156ab62c8a5a8a1ee5cded3dd909dcc618365 (patch) | |
| tree | 79149d2c37239d0633307a1f5f6e2b8e6a35c78c /challenge-004/abigail/lua/ch-2.lua | |
| parent | 7a1c751b9c8acb2cbea989419907a72cfcba3642 (diff) | |
| parent | 07d0ccdc3b3a291016a184219bb885bce49c5cd8 (diff) | |
| download | perlweeklychallenge-club-fe1156ab62c8a5a8a1ee5cded3dd909dcc618365.tar.gz perlweeklychallenge-club-fe1156ab62c8a5a8a1ee5cded3dd909dcc618365.tar.bz2 perlweeklychallenge-club-fe1156ab62c8a5a8a1ee5cded3dd909dcc618365.zip | |
Merge pull request #3668 from Abigail/abigail/week-004
Abigail/week 004
Diffstat (limited to 'challenge-004/abigail/lua/ch-2.lua')
| -rw-r--r-- | challenge-004/abigail/lua/ch-2.lua | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-004/abigail/lua/ch-2.lua b/challenge-004/abigail/lua/ch-2.lua new file mode 100644 index 0000000000..c1214ac67d --- /dev/null +++ b/challenge-004/abigail/lua/ch-2.lua @@ -0,0 +1,51 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-2.lua -f FILE < input-file +-- + +-- +-- Parse option, and exit if incorrect +-- + +local filename = "" +if #arg == 2 and arg [1] == "-f" +then filename = arg [2] +end + +if file == "" +then io . stderr : write ("Requires a '-f FILE' option\n") + os . exit (1) + end + +-- +-- Given a file name, and a set of letters, print +-- the words from the file which can be made with the letters. +-- +function extract_words (filename, letters) + for word in io . lines (filename) do + copy = word : gsub ("\n", "") : lower () + for i = 1, letters : len () do + -- + -- Remove each of the characters of 'letters' from + -- 'copy', after lowercasing, and only once. + -- + copy = copy : gsub (letters : sub (i, i) : lower (), "", 1) + end + -- + -- If we end up with the empty string, we have a winner. + -- + if (copy : len () == 0) + then + print (word) + end + end +end + +for line in io . lines () do + extract_words (filename, line : gsub ("\n", "")) +end |
