[Melbourne-pm] Roman numerals & assessing a CPAN module

Myf White myfwhite at gmail.com
Sat Dec 12 19:23:50 PST 2009

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 (http://compilers.iecc.com/comparch/article/07-03-118).

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.

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!)

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.

Myf White
mailto:myfwhite at gmail.com

"The most exciting phrase to hear in science, the one that heralds new
discoveries, is not 'Eureka!' but 'That's funny...'" -- Isaac Asimov

On Sat, Dec 12, 2009 at 3:12 PM, Andrew Savige <ajsavige at yahoo.com.au>wrote:

> MyfWhite wrote:
> > I need to convert roman numerals to arabic, and there seem to be
> > quite a few modules on the CPAN which can do this.
> Just in case you prefer a one liner to a CPAN module:
> use List::Util 'reduce';
> sub roman_to_dec {
>  reduce{$a+$b-$a%$b*2}map{10**(7&69303333/ord)%9995}split//,uc(shift)
> }
> Only works for "modern" Roman Numerals in 1-3999 range ... and I
> admit maintainability and readability may suffer. :)
> A while back, I wrote up a node describing the algorithm behind this
> monstrosity and even gave it a name, the PGA-TRAM algorithm:
>  http://www.perlmonks.org/?node_id=771219
> Cheers,
> /-\
> ------------------------------
> See what's on at the movies in your area. Find out now<http://au.rd.yahoo.com/movies/mailtagline/*http://au.movies.yahoo.com/session-times/>
> .
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20091213/c19fc30f/attachment.html>

More information about the Melbourne-pm mailing list