From fe7759f1063d132eb4fe234f95cc25b7d924b6dd Mon Sep 17 00:00:00 2001 From: Abigail Date: Sun, 7 Feb 2021 22:33:47 +0100 Subject: Lua solution for week 98, part 1 --- challenge-098/abigail/lua/ch-1.lua | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 challenge-098/abigail/lua/ch-1.lua diff --git a/challenge-098/abigail/lua/ch-1.lua b/challenge-098/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..4c8a8a4c92 --- /dev/null +++ b/challenge-098/abigail/lua/ch-1.lua @@ -0,0 +1,34 @@ +#!/opt/local/bin/lua + +-- +-- See ../README.md +-- + +-- +-- Run as: lua ch-1.lua < input-file +-- + +local content = {}; + +function readN (filename, amount) + if content [filename] == nil + then -- + -- Read the content of the file, strip the trailing + -- newline, and store the content in the table 'content' + -- + content [filename] = assert (io . open (filename, "r")) : + read ("*all") : + gsub ("\n$", "") + end + local out = content [filename] : sub (1, amount) + content [filename] = content [filename] : sub (amount + 1) + return out +end + +for line in io . lines () do + -- + -- Split the line on whitespace, call readN() with the + -- result, and print the return value of readN(). + -- + print (readN (line : match ("(%S+) (%S+)"))) +end -- cgit