Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

Comment on Caesar Shift
Download Code
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.

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-11-28 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (200 votes), past polls