Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

As you are new to Perl I think this is an excellent opportunity to start learning a couple of useful reflexes:

  • look at CPAN first, as already mentioned there is a Roman module, it is even reviewed on this site! So there is no need to re-invent this specific wheel.
  • use -w and use strict;, they will save you countless hours of chasing after silly bugs,
  • test your code! Get into the habit of always writing tests, write them even _before_ you start coding, then when your code passes all the test you know you are done

So here is a test for your code that shows that... it does not pass! Note that handling IIII as 4 can be considered optional, as it is only used on clock dials:

#!/usr/bin/perl -w use strict; while( <DATA>) { chomp; s{\s*#.*$}{}; # remove comments next unless( m{\S}); # skip empty lines my( $roman, $decimal)= split /\t/; unless( $decimal == decimal( $roman)) { warn "error $roman translated to ", decimal( $roman), " instea +d of $decimal\n"; } } sub decimal { $_= shift; # roman unless ( m/^ m{0,3} # thousands (cm|dc{0,3}|cd|c{0,3}) # hundreds (xc|lx{0,3}|xl|x{0,3}) # tens (ix|vi{0,3}|iv|i{0,3}) # ones $/ix) { die "That doesn't appear to be a well-formed Roman numeral.\n"; } my $val = 0; if (s/cm//i) {$val += 900;} if (s/cd//i) {$val += 400;} if (s/xc//i) {$val += 90;} if (s/xl//i) {$val += 40;} if (s/ix//i) {$val += 9;} if (s/iv//i) {$val += 4;} while (m/m/g) {$val += 1000;} while (m/d/g) {$val += 500;} while (m/c/g) {$val += 100;} while (m/l/g) {$val += 50;} while (m/x/g) {$val += 10;} while (m/v/g) {$val += 5;} while (m/i/g) {$val += 1;} return $val; } __DATA__ I 1 II 2 III 3 IV 4 V 5 VI 6 VII 7 VIII 8 IX 9 X 10 XI 11 XIV 14 # add more test here, V, C... MCM 1900 IIII 4 # for clocks

In reply to Re: Convert Roman numerals to decimal by mirod
in thread Convert Roman numerals to decimal by Anonymous Monk

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

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

    No recent polls found