aboutsummaryrefslogtreecommitdiff
path: root/challenge-097/dave-jacoby/perl/ch-1.pl
blob: 6de32af5c22d9dcf36316fe776f2fe27a4157722 (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
#!/usr/bin/env perl

use strict;
use warnings;
use feature qw{ say signatures state };
no warnings qw{ experimental };

use Getopt::Long;

my $n = 3;
my $s = 'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG';

GetOptions(
    'number=i' => \$n,
    'string=s' => \$s,
);

caesar_cipher( $s, $n );

sub caesar_cipher ( $s, $n ) {
    my @alpha = 'A' .. 'Z';
    my @bet   = @alpha;
    for ( 1 .. $n ) {
        unshift @bet, pop @bet;
    }

    my $alpha  = join '', @alpha;
    my $bet    = join '', @bet;
    my %cipher = map { $alpha[$_] => $bet[$_] } 0 .. $#alpha;

    $s = uc $s;

    my $t = join '', map { $cipher{$_} ? $cipher{$_} : $_ } split //, $s;

    say <<"END";

    INPUT:
        \$S = "$s", \$N = $n
    OUTPUT:
        "$t"
    
    Plain:  $alpha
    cipher: $bet

END

}