Thanks for all of the ideas. I've had a bit of a look into them, including testing them against some of the corner cases raised in the thread that Alfie linked to (<a href="http://compilers.iecc.com/comparch/article/07-03-118" target="_blank">http://compilers.iecc.com/comparch/article/07-03-118</a>).<br>
<br>I actually don't need numbers over 3999 (the ones I need to convert are all years in the late nineteenth century), so the limitations of some of the solutions are no problem.<br><br>Andrew's solution seems to be the easiest, and it handles the weird corner cases the best, but I prefer the extra line defining a hash that you have in your perlmonks post, than the less readable messing with ord results (clever, but I had to spend 10 minutes working out how it worked!)<br>
<br>I had a look at the roman numeral grammar, but unfortunately it's left recursive, so no good for Regexp::Grammars. I think maybe Damian mentioned in his talk that any left recursive grammar can be redefined to not be left recursive (or maybe I got this wrong and only some can). The mental gymnastics of doing this are something I learnt for an exam, and promptly forgot, so not worth trying to relearn on work's time. But definitely something I'd be interested in trying out in my own time.<br>
<br><br clear="all">Myf White<br>mailto:<a href="mailto:myfwhite@gmail.com" target="_blank">myfwhite@gmail.com</a><br><br>"The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!' but 'That's funny...'" -- Isaac Asimov<br>
<br><br><div class="gmail_quote">On Sat, Dec 12, 2009 at 3:12 PM, Andrew Savige <span dir="ltr"><<a href="mailto:ajsavige@yahoo.com.au" target="_blank">ajsavige@yahoo.com.au</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div style="font-family: Courier New,courier,monaco,monospace,sans-serif; font-size: 10pt;"><div><div>MyfWhite wrote:</div>
<div>> I need to convert roman numerals to arabic, and there seem to be</div>
<div>> quite a few modules on the CPAN which can do this.<br></div>
</div><div>Just in case you prefer a one liner to a CPAN module:</div>
<div> </div>
<div>use List::Util 'reduce';<br>sub roman_to_dec {<br> reduce{$a+$b-$a%$b*2}map{10**(7&69303333/ord)%9995}split//,uc(shift)<br>}</div>
<div> </div>
<div>Only works for "modern" Roman Numerals in 1-3999 range ... and I</div>
<div>admit maintainability and readability may suffer. :)</div>
<div> </div>
<div>A while back, I wrote up a node describing the algorithm behind this</div>
<div>monstrosity and even gave it a name, the PGA-TRAM algorithm:</div>
<div> </div>
<div> <a href="http://www.perlmonks.org/?node_id=771219" target="_blank">http://www.perlmonks.org/?node_id=771219</a></div>
<div> </div>
<div>Cheers,<br>/-\<br></div></div><br>
<hr size="1">
See what's on at the movies in your area. <a href="http://au.rd.yahoo.com/movies/mailtagline/*http://au.movies.yahoo.com/session-times/" target="_blank">Find out now</a>.</div></blockquote></div><br>