Support Forums
rot13 in perl - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Perl Programming Support (https://www.supportforums.net/forumdisplay.php?fid=31)
+---- Thread: rot13 in perl (/showthread.php?tid=3383)



rot13 in perl - wat - 12-06-2009

huzzah x 2!

Code:
#!/usr/bin/perl
use strict;
use warnings;

sub usage {
    print qq(
Usage:
$0 <text to encrypt>  <# of letter to rotate [13 by default]>
eg... $0 lolencryptthis 5

);
exit;
}

sub rot_encrypt {
    my ($text, $num_sub) = @_;
    $text = "\U$text";
    my %alphabet;
    
    for(my $i = 0x41; $i <= 0x5A; $i++) {
        my $sub = $i + $num_sub;

        while($sub > 0x5A) {
            $sub -= 0x1A;
        }
        $alphabet{chr($i)} = chr($sub);
    }

    my @text = split //, $text;

    for(my $i = 0; $i < @text; $i++) {

        $text[$i] = $alphabet{$text[$i]};
    }

    return join("",@text);
}

usage() if @ARGV != 2;

print rot_encrypt($ARGV[0], $ARGV[1]) . "\n";



RE: rot13 in perl - Socrates - 12-08-2009

Nice!!!!!!!!


RE: rot13 in perl - 0x80483fb - 12-12-2009

I picked this up, while checking out POE::Component::IRC Cookbook.
Code:
$rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];

Source: http://poe.perl.org/?POE_Cookbook/IRC_Bots


RE: rot13 in perl - wat - 12-12-2009

(12-12-2009, 06:56 AM)0x80483fb Wrote: I picked this up, while checking out POE::Component::IRC Cookbook.
Code:
$rot13 =~ tr[a-zA-Z][n-za-mN-ZA-M];

Source: http://poe.perl.org/?POE_Cookbook/IRC_Bots

Yeah, but that way's no fun. And you can get rid of the capitals if you supply the 'i' modifier.