diff options
| author | alicia.bielsa <aliciabielsa@gmail.com> | 2020-02-01 11:43:26 +0100 |
|---|---|---|
| committer | alicia.bielsa <aliciabielsa@gmail.com> | 2020-02-01 11:43:26 +0100 |
| commit | 24fd5bc104ad533fcfb7ace19d89d4d80f9a8df4 (patch) | |
| tree | 372653b349aa421fb589aacbc63f905fd45cea6d /challenge-045 | |
| parent | d094e986802beac5ebd557008a31d8a8032093b5 (diff) | |
| download | perlweeklychallenge-club-24fd5bc104ad533fcfb7ace19d89d4d80f9a8df4.tar.gz perlweeklychallenge-club-24fd5bc104ad533fcfb7ace19d89d4d80f9a8df4.tar.bz2 perlweeklychallenge-club-24fd5bc104ad533fcfb7ace19d89d4d80f9a8df4.zip | |
challenge-045
Diffstat (limited to 'challenge-045')
| -rw-r--r-- | challenge-045/alicia-bielsa/perl/ch-1.pl | 75 | ||||
| -rw-r--r-- | challenge-045/alicia-bielsa/perl/ch-2.pl | 12 |
2 files changed, 87 insertions, 0 deletions
diff --git a/challenge-045/alicia-bielsa/perl/ch-1.pl b/challenge-045/alicia-bielsa/perl/ch-1.pl new file mode 100644 index 0000000000..a2bfe07b85 --- /dev/null +++ b/challenge-045/alicia-bielsa/perl/ch-1.pl @@ -0,0 +1,75 @@ +#Square Secret Code +#The squate 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; +use Data::Dumper; + + +my $askForInput = 1; +my $errorMessage = ''; +my $columnLength = 8; + +while ($askForInput){ + + if ($errorMessage ){ + print "ERROR: $errorMessage\n"; + } + my $input = getInput(); + print "Input: '$input'\n"; + if ($input =~ /^q|quit$/i){ + print "Bye bye\n"; + $askForInput = 0; + } elsif ($input =~ /\S+/){ + $errorMessage = ''; + my $messageEncoded = encodeMessage($input); + print "\n>>>>>>>>>>>>>>>>Encoded message: $messageEncoded\n"; + } else { + $errorMessage = "ERROR, no valid message was entered\n"; + } + +} + +sub getInput { + print "------------------------------\n"; + print "Enter your message to be coded\nEnter quit(q) to exit\n"; + print "------------------------------\n"; + my $input = <STDIN>; + chomp($input); + return $input; +} + +sub encodeMessage { + my $message = shift; + my @aSubMessages = (); + my $messageEncoded = ''; + $message =~ s%\s+%%g; + my @aEncodedGroups = (); + my @aMessage = split ('', $message); + foreach my $indexMessage (0..$#aMessage){ + my $indexSubgroup = $indexMessage % $columnLength ; + unless (defined $aEncodedGroups[$indexSubgroup]){ + $aEncodedGroups[$indexSubgroup] = ''; + } + $aEncodedGroups[$indexSubgroup] .= $aMessage[$indexMessage]; #t + } + $messageEncoded = join(' ', @aEncodedGroups); + return $messageEncoded ; +} diff --git a/challenge-045/alicia-bielsa/perl/ch-2.pl b/challenge-045/alicia-bielsa/perl/ch-2.pl new file mode 100644 index 0000000000..cc4312fe2c --- /dev/null +++ b/challenge-045/alicia-bielsa/perl/ch-2.pl @@ -0,0 +1,12 @@ +#Write a script that dumps its own source code. +#For example, say, the script name is ch-2.pl then the following command should returns nothing. +#perl ch-2.pl | diff - ch-2.pl + +use strict; +use warnings; + +open (my $fh_file , '<', $0 ) or die "Error reading file"; +while (my $line = <$fh_file>){ + print $line; +} +close ($fh_file);
\ No newline at end of file |
