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

In regards to an old post about a caesar shift solution, I have tried my skills to approach this in an efficient way and came up with the following:
#!/usr/bin/perl -w # # Caesar shift decrypting script # Author: Aleksandr Melentiev <tzapper@users.sf.net> # Redistributable under the BSD License use strict; use warnings; print "Enter encrypted string:\n"; chomp(my $cipher = <STDIN>); my @alphabet = ('A' .. 'Z'); for (@alphabet) { my $text = $cipher; my $rot = join("", @alphabet); eval "\$text =~ tr/a-zA-Z/$rot/, 1" or die $@; print "ROT: $text\n"; push(@alphabet, shift(@alphabet)); }

Replies are listed 'Best First'.
Re: Caesar Shift
by tachyon (Chancellor) on Aug 25, 2004 at 10:47 UTC

    You can do it much more simply and without the arrays or the eval. Just rotate by 1 x 26 times and you are back to the start ie:

    $_ = "Hello\n"; for my $i(1..26){ tr/a-zA-Z/b-zaB-ZA/; print; }

    cheers

    tachyon

Re: Caesar Shift
by belg4mit (Prior) on Aug 25, 2004 at 05:12 UTC
    If I'm not mistaken, you should only need to do 25 rotations to present all possible results, the 26th being identity. This would then also allow the results to fit on a standard terminal without scrolling (assuming a message LTE 80 chars).

    --
    I'm not belgian but I play one on TV.

Re: Caesar Shift
by mpeg4codec (Pilgrim) on Aug 25, 2004 at 02:53 UTC
    #!/usr/bin/perl -w use strict; chomp(my $cypher = <STDIN>); $cypher = lc($cypher); $cypher =~ tr/a-z/n-za-m/; print "ROT: $cypher\n";

    Untested.

      The point of the OP's code is to apply all 26 possible shift widths.

      Makeshifts last the longest.