Front page | perl.perl6.language |
Postings from August 2006
My first functional perl6 program
Thread Next
From:
Mark J. Reed
Date:
August 23, 2006 15:40
Subject:
My first functional perl6 program
Message ID:
f60fe000608231539j3c5eb9d8yd4ae3f46821edb12@mail.gmail.com
To get my feet wet, I thought I'd translate my silly little cryptogram
helper. It turned out like this:
#!/usr/local/bin/pugs
#======================================================================
# 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{%key.keys.map({.lc,.uc})} = %key.values.map({.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.)
--
Mark J. Reed <markjreed@mail.com>
Thread Next
-
My first functional perl6 program
by Mark J. Reed