diff options
| author | Abigail <abigail@abigail.be> | 2021-01-26 02:52:09 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-01-26 12:07:41 +0100 |
| commit | 31f71b49569afa8b719e15047bb0d2392bbb7993 (patch) | |
| tree | f84181ccae97639c531e71b29dc2acc70040e651 /challenge-097/abigail/awk | |
| parent | cc68e1c2ea0d685072b4063aeb9af50f7e42cd98 (diff) | |
| download | perlweeklychallenge-club-31f71b49569afa8b719e15047bb0d2392bbb7993.tar.gz perlweeklychallenge-club-31f71b49569afa8b719e15047bb0d2392bbb7993.tar.bz2 perlweeklychallenge-club-31f71b49569afa8b719e15047bb0d2392bbb7993.zip | |
AWK solution for week 097, part 1
Diffstat (limited to 'challenge-097/abigail/awk')
| -rw-r--r-- | challenge-097/abigail/awk/ch-1.awk | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-097/abigail/awk/ch-1.awk b/challenge-097/abigail/awk/ch-1.awk new file mode 100644 index 0000000000..a33507ee33 --- /dev/null +++ b/challenge-097/abigail/awk/ch-1.awk @@ -0,0 +1,53 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-1.awk -s SHIFT < input-file +# + +BEGIN { + NR_OF_LETTERS = 26 + ORD_A = 65 + + # + # Create a letter to number mapping + # + for (i = ORD_A; i < ORD_A + NR_OF_LETTERS; i ++) { + t = sprintf ("%c", i) + ord [t] = i + } + + # + # Parse command line + # + for (i = 1; i < ARGC; i ++) { + if (ARGV [i] == "-s") { + shift = ARGV [i + 1] + } + } + ARGC = 0 +} + +{ + split($0, letters, "") + out = "" + # + # Iterate over the individual letters, shifting capital letters + # + for (i = 1; i <= length (letters); i ++) { + char = letters [i] + if (ord [char]) { + n = ord [char] - shift + if (n < ORD_A) { + n = n + NR_OF_LETTERS + } + char = sprintf ("%c", n) + } + out = out char + } + print out +} + |
