Pathologically Eclectic Rubbish Lister PerlMonks

### Rot13 Challenge

by PyrexKidd (Monk)
 on Feb 10, 2011 at 01:01 UTC Need Help??
PyrexKidd has asked for the wisdom of the Perl Monks concerning the following question:

I was browsing here and found this Rot13 Challenge.

Rot13 Encryption is performed by shifting the letters 13 characters to the right. A becomes N, B becomes O. The cool thing about this is an encrypted string can be decrypted by using the same algorithm. IOW, the A becomes N, N becomes A.

my first thought was to convert the characters to their equivalent ascii number, shift it, then return the ascii character.

```#!/usr/bin/perl

use strict;
use warnings;

use 5.010001;

my \$input;
say "Please Enter String a string to be encrypted or decrypted";
\$input = <>;
chomp \$input;
my @ascii_char;
my \$char;
while(\$input =~ m/(.)/g) {
push @ascii_char, ord \$1;
}

say @ascii_char;
open my \$ENCRYPTED_STRING, '>', \my \$encrypted_string;
foreach( @ascii_char ) {
if (\$_ >= 97 and \$_ <= 122) {
if ((122 - \$_) <13 ){
\$_ = 97 + (13 - (123 - \$_));
}
else {
\$_ += 13;
}
}
elsif (\$_ >= 65 and \$_ <= 90) {
if ((90 - \$_) < 13 ){
\$_ = 65 + (13 - (91 - \$_));
}
else {
\$_ += 13;
}
}

print \$ENCRYPTED_STRING chr;
}
close \$ENCRYPTED_STRING;

say \$encrypted_string;

Then I figured why not use the translate function...

```#!/usr/bin/perl
use strict;
use warnings;
use 5.010001;

say 'Please Enter a String to be Encrypted or Decrypted';
my \$input = <>;
chomp \$input;

\$input =~ tr/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/nopq
+rstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM/;
say \$input;

Considerably shorter.

So the challenge is this:

• Create the most succinct version
• Create the most obfuscated version.

Any takers?

Replies are listed 'Best First'.
Re: Rot13 Challenge
by eyepopslikeamosquito (Chancellor) on Feb 10, 2011 at 05:01 UTC

As you might expect, this has been golfed a few times before. For example:

The Qwika Perl wikipedia example program (which matches Anony's response above) is:
```perl -pe 'tr/A-Za-z/N-ZA-Mn-za-m/' < input_file > output_file
while the shortest Perl solution at shinh's golf site is:
```#!perl -p
y/N-ZA-z/A-za-m/

Re: Rot13 Challenge
by Anonymous Monk on Feb 10, 2011 at 01:51 UTC
\$ perl -pe'y/A-Za-z/N-ZA-Mn-za-m/' file
```my \$input;
print "Please Enter String to be encrypted or decrypted";
\$input = <>;
chomp \$input;
my @asciich;

while(\$input=~ m/(.)/g)
{push @asciich,ord (\$1);}
my \$count=scalar(@asciich);
print "Ascii values -----> @asciich\n";

for(my \$i=0;\$i<\$count;\$i++)
{
if ((\$asciich[\$i] >= 65 && \$asciich[\$i] <= 77)|| (\$asciich[\$i] >= 97 &
+& \$asciich[\$i] <= 109))
{
\$asciich[\$i] = \$asciich[\$i]+13;
}
else
{\$asciich[\$i] = \$asciich[\$i] - 13;}

}

print "Encrypted values -----> @asciich\n";

foreach(my \$i=0;\$i<\$count;\$i++)
{ \$asciich[\$i]=chr(\$asciich[\$i]);}
print "encrypted output ------> @asciich\t";
Re: Rot13 Challenge
by ELISHEVA (Prior) on Feb 10, 2011 at 07:52 UTC

Run instructions: place in a script, e.g. "myscript.pl" and run as follows: perl myscript.pl  "Hello World!"

```use strict;
use warnings;
package l_A'MN'm_N'mn;

our %sbernpu;
our @NETI;
our \$z_="what in the world is a";

\$_ = q{ \$sbernpu{\$#NETI}=[\$l_A'MN'm_N'mn'z_,"cevag","?"] };
\$! = y|=-},\"-'|    N-ZA-M{ } / n-za-m( ); @   \-|;  #cleanup

push @{\$sbernpu{\$#NETI}}, '';
print "@{\$sbernpu{\$#NETI}}\n";
Re: Rot13 Challenge
by sundialsvc4 (Abbot) on Feb 10, 2011 at 18:22 UTC

The “most obfuscatory” version... hmmm...

Option Explicit
...

;-)

Create A New User
Node Status?
node history
Node Type: perlquestion [id://887321]
Approved by mr_mischief
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2017-05-25 19:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (187 votes). Check out past polls.