Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Yet Another Cryptogram Script

by jwkrahn (Abbot)
on Mar 20, 2009 at 11:35 UTC ( [id://751994]=note: print w/replies, xml ) Need Help??


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

    Good idea. :) And thanks - you made me realize my understanding of the \U-style escapes was a little fuzzy.


    "Half of all adults in the United States say they have registered as an organ donor, although only some have purchased a motorcycle to show that they're really serious about it."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://751994]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-20 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found