Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I rarely define such subroutines without also defining the inverse. And once you have the inverse it seems like the most natural thing to do is use it to set up a straight forward table-driven version of the required routine:

#! perl -sw use 5.010; use strict; use constant _1to9 => [ '', qw[ I II III IV V VI VII VIII IX ] ] +; use constant _10to90 => [ '', qw[ X XX XXX XL L LX LXX LXXX XC ] ] +; use constant _100to900 => [ '', qw[ C CC CCC CD D DC DCC DCCC CM ] ] +; sub dec2roman { my $dec = shift; die "Bad input' unless $dec =~ m[^[0-9]+$] and $dec < 4e3; my @decDigits = split '', $dec; my $roman = ''; $roman .= 'M' x shift @decDigits if @decDigits == 4; $roman .= _100to900->[ shift @decDigits ] if @decDigits == 3; $roman .= _10to90-> [ shift @decDigits ] if @decDigits == 2; $roman .= _1to9-> [ shift @decDigits ] if @decDigits == 1; return $roman; } sub roman2dec { state %roman2dec; %roman2dec = map{ dec2roman( $_ ), $_ } 1 .. 3999 unless %roman2de +c; my $roman = uc shift; return $roman2dec{ $roman } if exists $roman2dec{ $roman }; die "Input '$roman' out of range (1..3999 (roman) inclusive)"; } for my $r ( qw[ XLII LXIX mi MMMCMLXXXVIII ] ) { printf "%15s : %d\n", $r, roman2dec($r); }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Rosetta PGA-TRAM by BrowserUk
in thread Rosetta PGA-TRAM by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (8)
    As of 2014-09-22 06:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (182 votes), past polls