blob: d674cdf7c28c16b17265607a27c230bcbbefa8af (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#!/opt/local/bin/lua
--
-- See ../README.md
--
--
-- Run as: lua ch-1.lua -s SHIFT < input-file
--
local shift = -1
local NR_OF_LETTERS = 26
local ORD_A = string . byte ("A")
--
-- Parse option, and exit if incorrect
--
if #arg == 2 and arg [1] == "-s"
then shift = arg [2] % NR_OF_LETTERS
end
if shift < 0
then io . stderr : write ("Requires a '-s SHIFT' option\n")
os . exit (1)
end
--
-- Shift a capital letter 'shift' places to the left,
-- rotating if shifted before 'A'
--
function do_shift (char, shift)
local n = string . byte (char) - shift
if n < ORD_A
then n = n + NR_OF_LETTERS
end
return string . char (n)
end
--
-- Shift capital letters
--
for line in io . lines () do
io . write (string . gsub (line, "[A-Z]", function (ch)
return do_shift (ch, shift)
end), "\n")
end
|