Sun, 15 Jul 2007

Rot-10 + Rot-3 = Rot-13

Sometimes I read the blogs of strangers and it inspires me.

Last night I read a tumblelog called Anarchaia which I think I probably found through some other stranger's blog on my curious staggers through the thoughts of people who code. In particular, the highlighted Thought on that page got me to thinking. So I tried scratching the itch in code and this is what fell out.

 #!/usr/bin/perl
 # splitrot by Shannon Prickett <binder@manjusri.org>
 # rotate vowels separately from consonants

 use strict;
 use warnings;

 #use Smart::Comments;

 my $consonant_string   = 'bBcCdDfFgGhHjJkKlLmMnNpPqQrRsStTvVwWxXzZ';
 my $vowel_string = 'aAeEiIoOuUyY';

 while (<>) {
     chomp;
     for my $letter (split //, $_) {
         my $rotted;
         if ($letter !~ qr{[$vowel_string]}msx) {
             $rotted = rot_n({ character => $letter, offset => 10, string =>
             \$consonant_string  });
         }
         else {
             $rotted = rot_n({ character => $letter, offset => 3, string =>
             \$vowel_string });
         }
         print $rotted;
     }
     print "\n";
 }

 sub rot_n {
     my $arg_ref = $_[0];
     my $character           = ${arg_ref}->{character};
     ### $character
     my $offset              = ${arg_ref}->{offset};
     my $letter_string_ref   = ${arg_ref}->{string};
     my $letter_string       = $$letter_string_ref;
     ### $letter_string

     if ($character =~ m{ [[:space:]|[:punct:]] }msx) {
         return $character;
     }

     my $character_index = index( $letter_string, $character );
     ### $character_index
     my $result_index = $character_index + ($offset * 2);
     ### $result_index
     if ($result_index > (length( $letter_string ) - 1)) {
         $result_index -= ($offset * 4);
     }
     ### $result_index

     my $return_letter = substr( $letter_string, $result_index, 1);
     ### $return_letter
     return $return_letter;
 }

That's not as horrible as I had feared it would be when all was said and done. Some examples:

 binder@death:~/src/r13$ ./splitrot 
 A man, a plan, a canal, SUEZ! 
 O zob, o cxob, o pobox, GEUM!
 O zob, o cxob, o pobox, GEUM!
 A man, a plan, a canal, SUEZ!
 You mean it just contradicts me all day?
 Iae zuob yh vegh pabhfoqyphg zu oxx qoi?
 Iae zuob yh vegh pabhfoqyphg zu oxx qoi?
 You mean it just contradicts me all day?
 binder@death:~/src/r13$

See? Reversible. Pronounceable? I don't think so. But you're free to pabhfoqyphg zu if you dare.

posted at 07:31 PDT (-0700)     (comments disabled)   permanent link   Technorati tagged as: ,
July
Sun Mon Tue Wed Thu Fri Sat
15
       
2007
Months
Jul