#!/usr/bin/perl -w use strict; while( ) { 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), " instead 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