To get my feet wet, I thought I'd translate my silly little cryptogram
helper.  It turned out like this:

# Braindead cryptogram assistant with hard-coded key.
my %key =
   a => 'f', b => 'o', e => 'n', f => 'w', g => 'd', h => 'a', j => 'm',
   k => 'i', l => 's', m => 'l', n => 'c', o => 'g', q => 'r', r => 'h',
   s => 'k', t => 'u', u => 'p', v => 't', w => 'j', x => 'e', y => 'v',

# Start by mapping all letters to question marks so the as-yet-undeciphered
# letters will stand out.  Then add the letters from the key, making sure to
# get both lowercase and uppercase forms

my %trans = ('a'..'z') »=>« ('?' xx 26);

%trans{{.lc,.uc})} ={.lc,.uc});

for =<> { say(.trans(%trans)) }

Surprisingly terse.  I wonder if there a more elegant way to do that
hash assignment?

It was especially convenient that String#trans accepts a Hash for the
mapping; my Perl5 and Ruby versions also store the key in a hash, but
then use keys+join and values+join to get tr-friendly strings out of
it.   This was much more natural.

(Speaking of which, pugs apparently doesn't have C<tr> as a global
function, only the .trans method)

It does sadden me somewhat that the say() requires the parens (or an
explicit $_ etc).  But I'll live. :)

(The key above is for today's Order of the Stick, btw.)


Reply via email to