Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Caesar Shift

by TZapper (Acolyte)
on Aug 25, 2004 at 01:18 UTC ( #385552=CUFP: print w/replies, xml ) Need Help??

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 <> # 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; }



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";


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

      Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://385552]
Approved by ybiC
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2020-02-26 01:11 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (113 votes). Check out past polls.