http://www.perlmonks.org?node_id=887321

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:

Any takers?