diff options
| author | Abigail <abigail@abigail.be> | 2021-02-12 20:20:13 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-04 18:57:26 +0100 |
| commit | d24bdea94e208042810202f4c7ac404ae820779e (patch) | |
| tree | 3f018199b70a2505fa292094b31f3ac2f5f5d4e8 /challenge-004/abigail/lua/ch-2.lua | |
| parent | 6d0ccb2c3e17a270f882d04098b3495d903a4b9d (diff) | |
| download | perlweeklychallenge-club-d24bdea94e208042810202f4c7ac404ae820779e.tar.gz perlweeklychallenge-club-d24bdea94e208042810202f4c7ac404ae820779e.tar.bz2 perlweeklychallenge-club-d24bdea94e208042810202f4c7ac404ae820779e.zip | |
Lua solution for week 4, part 2
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 |
