aboutsummaryrefslogtreecommitdiff
path: root/challenge-045/andrezgz/perl/ch-1.pl
blob: 774899857d9ef67f7baaa7aecacec20557b71a26 (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
#!/usr/bin/perl

# https://perlweeklychallenge.org/blog/perl-weekly-challenge-045/
# Task #1
# Square Secret Code
# The square secret code mechanism first removes any space from the original message.
# Then it lays down the message in a row of 8 columns.
# The coded message is then obtained by reading down the columns going left to right.
#
# For example, the message is "The quick brown fox jumps over the lazy dog".
#
# Then the message would be laid out as below:
#
# thequick
# brownfox
# jumpsove
# rthelazy
# dog
# The code message would be as below:
#
# tbjrd hruto eomhg qwpe unsl ifoa covz kxey
#
# Write a script that accepts a message from command line
#  and prints the equivalent coded message.

use strict;
use warnings;

# only lower-case letters from arguments form a string
my $msg = join '',
          map {my $w = lc $_; $w =~ s/[[:^lower:]]//g; $w }
          @ARGV or die "USAGE: $0 <message>";

my %cols;
# each letter is appended to the corresponding column
$cols{$_ % 8} .= substr $msg, $_, 1 for (0 .. (length $msg) -1 );

# coded message is formed by printing each column string in order
print join ' ', map { $cols{$_} } sort keys %cols;

__END__

./ch-1.pl The quick brown fox jumps over the lazy dog
tbjrd hruto eomhg qwpe unsl ifoa covz kxey