Skip to content

Instantly share code, notes, and snippets.

@adamcrussell
Created February 2, 2020 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamcrussell/b67df3b76c00d4b37fb85b033b707434 to your computer and use it in GitHub Desktop.
Save adamcrussell/b67df3b76c00d4b37fb85b033b707434 to your computer and use it in GitHub Desktop.
Perl Weekly Challenge 045
use strict;
use warnings;
##
# Write a script that accepts a message from the
# command line and prints the equivalent square secret
# coded message.
##
use constant SQUARE_SIZE => 8;
sub encode{
my($message) = @_;
$message =~ tr/ //d;
my $encoded;
my @buckets;
my @characters = split(//, lc($message));
for my $i (0 .. @characters){
$buckets[$i % SQUARE_SIZE] = [] if !$buckets[$i % SQUARE_SIZE];
push @{$buckets[$i % SQUARE_SIZE]}, $characters[$i] if $characters[$i];
}
for my $bucket (@buckets){
$encoded .= join("", @{$bucket}) . " ";
}
return $encoded;
}
MAIN:{
my $message = $ARGV[0];
print encode($message) . "\n";
}
print<<''x2,"\n"
print<<''x2,"\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment