in reply to Yet Another Cryptogram Script
sub encode { my ($text, @alphabet) = @_; local $" = ''; eval "\$text =~ tr/a-z/@alphabet/"; $_ = uc for @alphabet; eval "\$text =~ tr/A-Z/@alphabet/"; $text } sub decode { my ($text, @alphabet) = @_; local $" = ''; eval "\$text =~ tr/@alphabet/a-z/"; $_ = uc for @alphabet; eval "\$text =~ tr/@alphabet/A-Z/"; $text }
Instead of running eval twice per subroutine you could do it like this:
sub encode { my ($text, @alphabet) = @_; local $" = ''; eval "\$text =~ tr/a-zA-Z/\L@alphabet\U@alphabet\E/"; $text } sub decode { my ($text, @alphabet) = @_; local $" = ''; eval "\$text =~ tr/\L@alphabet\U@alphabet\E/a-zA-Z/"; $text }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Yet Another Cryptogram Script
by missingthepoint (Friar) on Mar 20, 2009 at 12:17 UTC |